views:

39

answers:

2

I have a table being populated and I would like the td id to be the id number for the data that was returned. How do I assign an id to a td from jquery?

var $tr = $('<tr><td class="name"></td></tr>').insertAfter('#newTable tr:last');
$tr.find('.name').html(data.Name);
//How do I assign data.id to <td class="name"> ?
+2  A: 

This should work:

$tr.find('.name').attr('id', data.id).html(data.Name);
Nate Pinchot
+1  A: 
$('.name').attr('id', data.id)
William
This will affect *all* `.name` elements currently on the page. :o)
patrick dw
sure. But my understanding of his question was "how to set the id of an element with JQuery".
William
@William - Well, sort of. The question is how to set the ID of a specific element. If the OP was to run your code, *all* elements on the page that have the class `name` would get that ID. This isn't what the OP wanted, not to mention the fact that it would be invalid HTML since IDs must be unique. :o)
patrick dw
@patrick point taken :)
William