Skip to content Skip to sidebar Skip to footer

How To Append The Same Variable Twice?

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 d

Solution 1:

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.

Solution 2:

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');

Post a Comment for "How To Append The Same Variable Twice?"