views:

45

answers:

1

Here's the rundown:

I am creating a table dynamically 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?

+1  A: 

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'.

BalusC
Thank you BalusC, I can't believe it was as simple as using parents isntead of parent.. I thought parent() had that functionality.. thanks again
trembaly
Just like I suggested 20 minutes ago, beautiful.
ILMV
One word of warning about using parents() is that if you have nested tables, you will get a collection of all table cells that are in the parent hierarchy. closest(), if you're using a recent version of jQuery, will be better and faster, and ensure that you're only getting the closest parent tr.
jmar777
@jmar: That's exactly why I said that `closest('tr')` is better. The `parents('tr')` returns **all** parents matching `'tr'`.
BalusC
@BalusC: Right - I was agreeing. I was just providing the reasoning behind the assertion that .closest() was better here, in case the OP isn't as familiar with the jQuery API.
jmar777
@jmar: ah yes, I see, thanks :) I extended the answer with the key difference explanation anyway.
BalusC