• 0

Need Help in jquery


Question

i have the below code to dynamically add a tr node.

var s="\"<tr>\""

$('#s').attr('id','tr1').appendTo('#target');

but it doesnt works. instead of that the below said line works fine.

$('<tr>').attr('id','tr1').appendTo('#target');

please help to fix this.

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

i have the below code to dynamically add a tr node.

var s="\"<tr>\""

$('#s').attr('id','tr1').appendTo('#target');

but it doesnt works. instead of that the below said line works fine.

$('<tr>').attr('id','tr1').appendTo('#target');

please help to fix this.

that's because your first attempt is completely wrong, you're creating a string, with unnecessary extra quotes, and doing nothing with it, then you're selecting every element in the document with id="s", changing their attributes, and then trying to move them elsewhere in the dom.

the alternative is correct (although i think you perhaps should close the tag)

if you really must use a variable then:

var s="&lt;tr&gt;&lt;tr/&gt;";
$(s).attr('id','tr1').appendTo('#target');

Link to comment
Share on other sites

This topic is now closed to further replies.