jonnie
jonnie

Reputation: 12710

How do I reference a child of an eq() jquery

I have a table I'm trying to derive a JSON object from using the below code

var tbl = $('#myTable tr:has(td)').map(function(i,v){
    var $td = $('td', this);
    return{
        id:$td.eq(0).text(),
        column1:$td.eq(1).text(),
        column2:$td.eq(2).text()

    }
    }).get();

This works perfect except from one thing, I sometime have a textbox or checkbox inside a td whose value I need to retrieve. I've Google'd and searched StackOverflow but I could not find any that worked in this situation

I have also tried the follow with no luck

id:$td.eq(0).val()
id:$td.eq(0).childern().val()

any suggestions or advice would be greatly appreciated

Upvotes: 1

Views: 538

Answers (2)

Michael Malinovskij
Michael Malinovskij

Reputation: 1422

If you need value and text then use something like this:

id:$td.eq(0).find(':checkbox').attr('value') + $td.eq(0).text();

If you need value only then:

id:$td.eq(0).find(':checkbox').attr('value');

By the way, jQuery team recommended (check comments below) using .find() instead of $(target, context); In your example:

var $td = $('td', this);

should look like:

var $td = $(this).find('td');

Upvotes: 0

Jamiec
Jamiec

Reputation: 136134

You'll probably want to find the input if its something like a textbox:

$td.eq(0).find('input').val()

for a checkbox, use the pseudo-selector :checkbox and determine its checked property

$td.eq(0).find(':checkbox').prop('checked')

Upvotes: 2

Related Questions