Felipe Mosso
Felipe Mosso

Reputation: 3927

How can I get each cell value of each row using jQuery

I'm adding rows dinamically in my WebPage and at one moment I want to get the value of each cell in a row.

This is the declaration of my row:

<tr class="template-upload fade">
<td class="name"><span>{%=file.name%}</span></td>
<td class="title"><label>Andar: <input name="andar" required></label></td>
</tr>

As you see, my rows have two cells: name and title. I'm trying to get the value of "name" cell and value of the input "andar" using jQuery.

This is what I have so far:

$('.template-upload tr').each(function () {
        var item = $(this); //this should represent one row

        var name = item.find('.name').text();
        var andar = item.find('input:text[name=andar]').val();
});

However, I'm not getting anything in this two variable.

What could I be missing here?

Upvotes: 0

Views: 3395

Answers (5)

Octopus
Octopus

Reputation: 8325

You have a typo...

$('tr.template-upload').each(function () {
    ...
});

Upvotes: 0

u.k
u.k

Reputation: 3091

.template-upload IS your tr

Change your selector to something like:

$('.template-upload').each(function(node){ /* logic */ }

Upvotes: 0

Aaron Powell
Aaron Powell

Reputation: 25099

Are you sure your selector is right? Looking at it your selector is .template-upload tr which means you're looking for the tr elements inside the element with the .template-upload class.

I think you'd be wanting:

$('tr.template-upload').each(...)

Upvotes: 0

Christopher Marshall
Christopher Marshall

Reputation: 10736

Remove tr from your selector.

$('.template-upload')

Upvotes: 0

j08691
j08691

Reputation: 207901

You were close, you just needed to remove the tr from your selector. Your row already has the class .template-upload so $('.template-upload tr') doesn't exist:

$('.template-upload').each(function () {
        var item = $(this); //this should represent one row
        var name = item.find('.name').text();
        var andar = item.find('input:text[name=andar]').val();
});

jsFiddle example

Upvotes: 1

Related Questions