rao
rao

Reputation: 223

how to get row's id of table through jquery

this is my table HTML

<table id="list" class="ui-jqgrid-btable" border="0" cellspacing="0" cellpadding="0" tabindex="0" role="grid" aria-multiselectable="false" aria-labelledby="gbox_list" style="width: 1702px;">
 <tbody>
 <tr class="jqgfirstrow" style="height:auto" role="row">
 <tr id="141" class="ui-widget-content jqgrow ui-row-ltr" tabindex="-1" role="row">
 <tr id="144" class="ui-widget-content jqgrow ui-row-ltr" tabindex="-1" role="row">
 <tr id="147" class="ui-widget-content jqgrow ui-row-ltr ui-state-highlight"    tabindex="0" role="row" aria-selected="true">
</tbody>
 </table>

i want to get the ids of all three rows(i.e 141,144,147) using jquery. i have tried .find .closest but uptill now havent got any success.

Upvotes: 0

Views: 1384

Answers (4)

Ajinkya
Ajinkya

Reputation: 22710

You can use Has-Attribute selector

$("#list tr[id]").each(function(){
   // Using alert just to show the result
   alert(this.id);
});

This will select tr which have id attribute

Fiddle

Upvotes: 1

Sandeeproop
Sandeeproop

Reputation: 1776

Try this

$(document).ready( function() {
  var idArr = [];
  $("tr").each(function(){
    if($(this).attr("id")){
      idArr.push($(this).attr("id"));
    }      
  });
  console.log(idArr)
});

Here is the a fiddle for above code

Upvotes: 0

user3497034
user3497034

Reputation:

$("#list tr").each(function(obj){
 alert(obj.id);
});

Upvotes: 0

hsz
hsz

Reputation: 152206

Try with:

var ids = $('#list').find('tr').map(function(){ return $(this).attr('id'); });

or:

var ids = $('#list').find('tr[id]').map(function(){ return this.id; });

Output:

[141, 144, 147]

http://jsfiddle.net/KbEyk/

Upvotes: 0

Related Questions