jQuery - Adding a line break when value is added to textarea

luke-sully

At present, when you click on the dropdown, select an option and click on any of the checkboxes, it adds a value to the textbox. However when I add a second value, it's entered directly next to the first value. How would i go about adding a line break?

Fiddle

jQuery

 $(document).ready(function () {
        $('.group').hide();
        $('#selectMe').change(function () {
            $('.group').hide();
            $('#' + $(this).val()).show();
        })
    });

    var courses = {
        course1: 'BIS 445 UCC lvl8 ',
        course2: 'Commerce 475 UCC lvl8 ',
        course3: 'Economics 350 UCD lvl8 ',
        course4: 'Government 405 UL lvl8 ',
        course5: 'Medicine 600 UCC lvl8',
        course6: 'Nursing 500 UCC lvl8'
    }

    var $mytextbox = $('#courses');

    $("input[type='checkbox'][name*='course']").change(function () {
        var $chk = $(this);
        if ($chk.prop('checked')) {
            $mytextbox.val($mytextbox.val() + courses[$chk.attr('name')]);
        } else {
            $mytextbox.val($mytextbox.val().replace(courses[$chk.attr('name')], ""));
        }
    });
War10ck

In <textarea> inputs, a line break is created by inserting a \n at the end of the line, thus creating a new line.

Modify your code to look like the following:

$("input[type='checkbox'][name*='course']").change(function () {
    var $chk = $(this);
    if ($chk.prop('checked')) {
        $mytextbox.val($mytextbox.val() + courses[$chk.attr('name')] + "\n");
    } else {
        $mytextbox.val($mytextbox.val().replace(courses[$chk.attr('name')] + "\n", ""));
    }
});

I've updated your JSFiddle here to demo how this works. It's important to remember to update both the insert and removal of the \n character as the courses are checked and unchecked. Without inserting and removing the \n character in both places, you may experience undesired results such as multiple lines between courses.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Insert line break inside placeholder attribute of a textarea?

Set value of textarea in jQuery

Disable submit button when textarea added dynamically is empty - jQuery

Textarea line break no working properly

Javascript append text to textarea with line break

Make textarea with setted height grow when new line is added

HTML textarea to javascript and keep line break

Pressing ENTER in <textarea> to break a line

Prevent Typing a Line Break in textarea Input With onkeyup="this.value=this.value.replace..."

In angular using interpolation for assigning the value of title of span element in HTML.Adding &#13; for a line break is not working

TextArea line break causing ERROR: value too long for type character varying(1000)

Enable Break Line or New Line in Textarea when press enter

jQuery each line in textarea

adding class to jquery added numbers

jQuery: add line break at cursor position when hitting Enter

jQuery post textarea wrap hard not show line break

Textarea line break after ';" character

jQuery adding dynamically autosize textarea

Textarea doesn't break line on 'enter' press

How to auto break line when content is too long in Jquery?

Unable to get Dynamically added textarea value on keypress

Adding line break in NodeAttributes in OmniXML

No automatic line break when exceeding textarea in form input field

How to make a line break in a textarea?

How to set textarea value with line break in ReactJS

Adding a line break in string variable

Make text inside textarea not break to next line

Split value from textarea by line break and add text indent to each of them (Javascript)

Input type="textarea". Break line at the end of the line