Tiep Pt
Tiep Pt

Reputation: 75

Get data attributes in table all tr

I have table like this:

  <tr class="alignC even" data-pdp-id="10" role="row"> 
  <tr class="alignC even" data-pdp-id="11" role="row">
  <tr class="alignC even" data-pdp-id="12" role="row"> 
  <tr class="alignC even" data-pdp-id="13" role="row"> 
  <tr class="alignC even" data-pdp-id="14" role="row">

How can I get an array contains all data-pdp-id with jquery

Upvotes: 2

Views: 14048

Answers (4)

TinDora
TinDora

Reputation: 390

You can use each to loop through tr element, inside each iteration, you can get the pdp-id, then push it in pre-defined array.

var pdpArr = [];
$('.alignC').each(function() {
    pdpArr.push($(this).data('pdp-id'));
});
console.log(pdpArr); // You can get the array of pdp-id at this point

Upvotes: 3

You can use either .attr("data-pdp-id") or .data("pdp-id")

Then when you have the data push it to an arr. arr.push($(this).data("pdp-id"))

var arr = []
$("table tr.alignC").click(function() {
  console.log($(this).attr("data-pdp-id"))
})


$("table tr").each(function() {
  arr.push($(this).attr("data-pdp-id"))
})

console.log(arr)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr class="alignC even" data-pdp-id="10" role="row">
    <td>10</td>
  </tr>
  <tr class="alignC even" data-pdp-id="11" role="row">
    <td>11</td>
  </tr>
  <tr class="alignC even" data-pdp-id="12" role="row">
    <td>12</td>
  </tr>
  <tr class="alignC even" data-pdp-id="13" role="row">
    <td>13</td>
  </tr>
  <tr class="alignC even" data-pdp-id="14" role="row">
    <td>14</td>
  </tr>
</table>

Upvotes: 2

4b0
4b0

Reputation: 22323

You can loop and get that value using data and push that value in array.

var ID=[];
$('#TableID').find('tr').each(function() {
  ID.push($(this).data('pdp-id'));
});

Upvotes: 1

ashanrupasinghe
ashanrupasinghe

Reputation: 756

var ids=[];
    $('#tableId tr').each(function(){
      ids.push($(this).attr('data-pdp-id'))
    })

check this one

Upvotes: 0

Related Questions