Bishal Paudel
Bishal Paudel

Reputation: 1946

Jquery checkbox.checked always returns false

$("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.

Upvotes: 4

Views: 14079

Answers (4)

Kerem Kulak
Kerem Kulak

Reputation: 51

You are using current_checkbox.checked, checkbox has not any 'checked' properties, but you can use $('#id').is(':checked')

Upvotes: 5

Sterling Archer
Sterling Archer

Reputation: 22425

You're using a jQuery element, not a DOM element. Use:

if(current_checkbox.prop("checked")) {

} //just for Steve <3

Upvotes: 2

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324790

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.

Upvotes: 1

Blazemonger
Blazemonger

Reputation: 92983

jQuery objects do not have a checked property. Replace

current_checkbox.checked

with

current_checkbox.prop('checked')

http://api.jquery.com/prop

Upvotes: 11

Related Questions