jQuery: How to get ID of dynamically generated element?
Here's the rundown:
I am creating a table dynamicall开发者_StackOverflow社区y from the results of an AJAX call. To add the rows, I am using the "after" function, seeing as there are certain rows in the table that function as headers.
After I add these rows, I have a quantity box that I would like to make editable. I am able to add an event to it, however I can't get the ID of my row ($(this).parent('tr').attr('id') doesn't work - it returns undefined.
How would I go about being able to get the value of the ID for an element that was added after the DOM tree was parsed?
The parent('tr')
returns only the direct parent. To be a direct child of a <tr>
, the this
should be a <td>
. But the .linkQuantite
seems to be an <a>
element. This is definitely not a direct child of <tr>
. You would rather like to use parents('tr')
or better closest('tr')
for this. The key difference is that parents('tr')
returns all parents matching 'tr'
while closest('tr')
returns only the first parent matchnig 'tr'
.
精彩评论