AngryHacker
AngryHacker

Reputation: 61616

How to locate the value of the first textbox in the table?

I have a JSFiddle: http://jsfiddle.net/dPAAG/

I can loop through the rows which have the checkbox checked...

$('#ordertable').find('input[type="checkbox"]:checked').each(function () {
    //this is the current checkbox
    var row = $(this);

    // this is null
    var stuff = row.find('.productVal').val();

    alert('this is a checked checkbox');
});

But I can't seem to get to the value of the first textbox in the same row. I am trying to access it by using the class name, but it does not seem to work. The variable is always undefined.

Am I missing something simple?

Upvotes: 0

Views: 58

Answers (2)

Leth0_
Leth0_

Reputation: 534

I have changed your fiddle a bit, The code speaks for itself but if you don't understand any part of it please ask as I'm more than happy to explain it.

I assume you want the value of the first column and not the second column, if you wish to have the second column then you can easily edit the code and change [0] to [1] after the .children() function.

http://jsfiddle.net/dPAAG/2/

    $(function () {
        $('#go').on('click', function () {
            findCheckboxes();
        });
    });


    function findCheckboxes() {


        $('#ordertable').find('input[type="checkbox"]:checked').each(function () {
            //this is the current checkbox
            var row = $(this);
            var itemValue = $($(this).parent().siblings()[0]).children().val();
            console.log(itemValue);
             //var stuff = row.find('.productVal').val();

            //alert('this is a checked checkbox');
        });

    }

Upvotes: 1

Ram
Ram

Reputation: 144689

row is a reference to an input which can't have descendant elements. You are missing the closest method for selecting the closest tr parent of the input:

var row = $(this).closest('tr');

You can also use the filtering has method:

$('#ordertable tr').has('input[type="checkbox"]:checked').each(function () {
    // `this` here refers the matching `tr` element
});

http://jsfiddle.net/7Tr87/

Upvotes: 1

Related Questions