Reputation: 1833
I have table
<tr>
<td id="id">2</td>
<td id="type"><span class="label">snippets</span></td>
<td id="name">all</td>
<td id="time">None</td>
<td id="status">None</td>
<td id="actions"><i class="icon-refresh" id="refresh-parser"></i></td>
</tr>
And i have this jQuery code
$(document).on('click', '#refresh-parser', function(){
var before_content = $($(this).parent());
var parser_id = $('#refresh-parser').parent().parent().children("td:first");
var parser_time = $('#refresh-parser').parent().parent().children("td")[3];
//$($(this).parent()).html('<div class="loading"></div>');
});
Yes, this code works, but how i can do it by id
on my td
elements?
Thanks and sorry for such silly question...
Upvotes: 0
Views: 677
Reputation: 123739
Id's are supposed to be unique in the document. So you should just use:-
$('#id'),$('#name') etc...
So, to fix this as it looks like you have duplicate ids in your html, make the ids as class names if they are duplicated in the document, like below.
<tr>
<td class="id">2</td>
<td class="type"><span class="label">snippets</span></td>
<td class="name">all</td>
<td class="time">None</td>
<td class="status">None</td>
<td class="actions"><i class="icon-refresh" id="refresh-parser"></i></td>
</tr>
and access it like this:-
$(document).on('click', '#refresh-parser', function(){
var id = $(this).closest('tr').find('.id').text();
var name = $(this).closest('tr').find('.name').text();
// and so on...
});
Refer .closest()
Upvotes: 1
Reputation: 2376
When you want to get an element by ID you can just do
$('#id');
Then I see you use .parent().parent(). If I were you I would use closest('tr') for example. Then the javascript searches for the closest parent element that matches your criteria. You get a little cleaner code.
Upvotes: 0