Reputation: 250
I use this codes to loop through tr
$('#prod_tbl > tbody > tr').each(function () {
var x = $(this);
});
Now what i want is i have TD with different class names like .price,.name
I want access the value of each TD in different variable, when i loop through the TR
Like
$('#prod_tbl > tbody > tr').each(function () {
var x = $(this);
var price=$("td.price").text();
var name= $("td.name").text();
});
For each TR
Upvotes: 1
Views: 232
Reputation: 148110
You are currently selecting all the td with class price
and name
. Pass current tr
in context, jQuery( selector [, context ] ) or use find() to get the td in descendant of current row.
var price=$("td.price", this).text();
var name= $("td.name", this).text();
Using find()
var price = $(this).find("td.price").text();
var name = $(this).find("td.name").text();
Upvotes: 1
Reputation: 388316
use .find() to find the td
which are descendants of the current tr
element
$('#prod_tbl > tbody > tr').each(function () {
var x = $(this);
var price = $(this).find("td.price").text();
var name = $(this).find("td.name").text();
});
Upvotes: 1
Reputation: 67187
You have to use .find()
in this context to accomplish your task. That's because td
s are the descendants of every Tr
. $(this)
will points to the Tr
, so we have to use .find(selector)
to find the matched descendants of it,
Try,
$('#prod_tbl > tbody > tr').each(function () {
var x = $(this);
var price= x.find("td.price").text();
var name = x.find("td.name").text();
});
Please read here to know more about .find()
Upvotes: 1