I am trying to append the same variable twice but it appears the code only works on the second one.
var test=document.createElement('option');
test.innerHTML='some data';
$('#data1').append(test);
$('#data2').append(test);
I want to append the variable test twice WITHOUT creating another variable. Thanks a lot!
Try putting in same selector like below,
DEMO: http://jsfiddle.net/mrMva/
$('#data1, #data2').append(test);
As Felix pointed out from jQuery docs for .append
If there is more than one target element, however, cloned copies of the inserted element will be created for each target after the first.
Clone it:
$('#data1').append(test);
$('#data2').append(test.cloneNode(true));
Or just do this, and jQuery will clone it for you:
$('#data1, #data2').append(test);
Or like this:
$("<option>some data</option>").appendTo('#data1, #data2');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With