$("tbody input[name='rooms_to_book']").on('change',function(){
var current_row = $(this).closest("tr");
var current_checkbox = current_row.find("input[name=checkbox]");
if(current_checkbox.checked) {
var price_per_night = parseFloat(current_row.find("td[name=price]").text().replace('$',''));
var rooms_to_book = parseInt(current_row.find("input[name=rooms_to_book]").val());
rooms_to_book = rooms_to_book ? rooms_to_book : 0;
total_price = price_per_night * rooms_to_book;
}else{
total_price = 0;
}
current_row.find("td[name=total_price]").text('$'+ total_price);
});
The current_checkbox.checked
always returns false, the html is correct.
$("tbody input[name='rooms_to_book']").on('change',function(){
var current_row = $(this).closest("tr");
var current_checkbox = current_row.find("input[name=checkbox]");
if(current_checkbox.checked) {
var price_per_night = parseFloat(current_row.find("td[name=price]").text().replace('$',''));
var rooms_to_book = parseInt(current_row.find("input[name=rooms_to_book]").val());
rooms_to_book = rooms_to_book ? rooms_to_book : 0;
total_price = price_per_night * rooms_to_book;
}else{
total_price = 0;
}
current_row.find("td[name=total_price]").text('$'+ total_price);
});
The current_checkbox.checked
always returns false, the html is correct.
4 Answers
Reset to default 11jQuery objects do not have a checked
property. Replace
current_checkbox.checked
with
current_checkbox.prop('checked')
http://api.jquery.com/prop
You are using current_checkbox.checked, checkbox has not any 'checked' properties, but you can use $('#id').is(':checked')
You're using a jQuery element, not a DOM element. Use:
if(current_checkbox.prop("checked")) {
} //just for Steve <3
current_row.find("input[name=checkbox]")
I'm fairly sure this returns a jQuery object, not the underlying checkbox element.
if(current_checkbox[0].checked)
Should work.
checked
isn't a property of a jQuery object – crush Commented Jan 14, 2014 at 17:57