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
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?
text()method does not add content, it completely replaces it. Tryprepend()orappend()instead.