sambit.albus
sambit.albus

Reputation: 250

How could i find the text in a specific TD with a css class where TR is stored as this

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

Answers (3)

Adil
Adil

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.

Use context in selector

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

Arun P Johny
Arun P Johny

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

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67187

You have to use .find() in this context to accomplish your task. That's because tds 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

Related Questions