Hai Truong IT
Hai Truong IT

Reputation: 4187

How to get value from list table tr

HTML :

<table id="gwAssignForm">
    <tr data-id="1"></tr>
    <tr data-id="2"></tr>
    <tr data-id="3"></tr>
    <tr data-id="4"></tr>
    <tr data-id="5"></tr>
</table>

And javascript :

var product_id = [];
$("#gwAssignForm tr[data-id]").each(function () {
    product_id.push($(this).text());
});
var result = '"' + product_id.join('", "') + '"';

How to get list id, with result: 1, 2, 3, 4, 5

Upvotes: 1

Views: 59

Answers (1)

Tushar
Tushar

Reputation: 87203

text() will give you the innerText of the element. Use data().

Return the value at the named data store for the first element in the jQuery collection, as set by data(name, value) or by an HTML5 data-* attribute.

Use data('id') to get the data-id attribute value.

var product_id = [];
$("#gwAssignForm tr[data-id]").each(function() {
  product_id.push($(this).data('id'));
});

document.write(product_id);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<table id="gwAssignForm">
  <tr data-id="1"></tr>
  <tr data-id="2"></tr>
  <tr data-id="3"></tr>
  <tr data-id="4"></tr>
  <tr data-id="5"></tr>
</table>

Upvotes: 3

Related Questions