0

In JavaScript you could just use the =. or += to add existing data to data. How do I do this in jQuery. I am trying to add a check-box list to my form, each field should building up a list in the textarea separating data by a comma.

Can't seem to figure it. It just replaces the q value and does not add.

jQuery

function ExportButton(){
    var data = $('#orders').text();
    var selected = new Array();
        $('.mycb:checked').each(function() {
        var q = $(this).attr('value');
        alert(q);
        $('#orders').text(data+q+',');

    });
}

HTML

<div><input type="checkbox" id="mycb2" class="mycb" value="Chocolate" /> Chocolate</div>
<div><input type="checkbox" id="mycb1" class="mycb" value="Vanilla" /> Vanilla</div>
<a href="javascript:ExportButton()">Export to Textarea</a>
<textarea rows="10" cols="80" id="orders" name="orders"></textarea>

jsFiddle

http://jsfiddle.net/GJXKW/3/

Any help would be appreciated. I want to a build a list like 4,5,3,4,24,2424,2, in the textarea ending without the comma would be a bonus but I will do regex maybe?

1
  • jQuery's text() method does not add content, it completely replaces it. Try prepend() or append() instead. Commented May 15, 2013 at 15:02

5 Answers 5

4

Noone seems to recognize that this is inside a loop, and that text(), html() and val() will overwrite the content on each iteration. All you really have to do is concentenate the string in the loop, and append it once after the loop :

function ExportButton(){
  var data = "";

  $('.mycb:checked').each(function(i,el) {
      data += el.value + ',';
  });

  $('#orders').text( data );
}

FIDDLE

Sign up to request clarification or add additional context in comments.

2 Comments

+1 for detail and reading my question correctly. I added a blank liner to ensure the form is blank before running the function so that no data is ever duplicated.
@TheBlackBenzKid - You're welcome, and if you'd like to start with an empty textarea each time, just don't get it's value to begin with. I'll update the answer !
1

Use .val instead of .text jsFiddle

    function ExportButton(){
    var data;
    var selected = new Array();
        $('.mycb:checked').each(function() {
        var q = $(this).attr('value');  
            data = $('#orders').val();
            if(data ){
              data = data + "," +q 
            }else {
                  data = q 
            }
        $('#orders').val(data);

    });
}

1 Comment

Interesting to use an conditional statement so plus 1 for that.
0

It's just because jQuery doesn't recognize the text() of a <textarea>. As it is like an input field, you may use the val() function:

function ExportButton(){
    var data = $('#orders').val();
    var selected = new Array();
    $('#mycb:checked').each(function() {
        var q = $(this).attr('value');
        alert(q);
        $('#orders').val(data+q+',');
    });
}

Comments

0

Have a see at this http://jsfiddle.net/steelywing/GJXKW/8/

$('#export').click(function () {
    var checked = [];
    $('input:checked').each(function () {
        checked.push($(this).val());
    });
    $('textarea').val(checked.join(','));
});

Comments

-1

You can always intermix jQuery and vanilla JS, so if you have something that works, just do it and don't call the overhead of jQuery for a simple text node append.

1 Comment

What are you talking about?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.