Reputation: 61616
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
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.
$(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
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
});
Upvotes: 1