Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fire event on Jquery check/uncheck

I want to set a variable (pageselected) as per the checkbox click/unclick event.

The HTML code is :

<thead>
    <tr id="othercheckbox">
        <th width="10"><input type="checkbox" name="zip" class="all"  value="all" /></th>              
    </tr>
</thead>
    

The code in JS file is :

$('#othercheckbox').click(function() {

    if($(this).is(':checked')){
        console.log("CCCCheckeddddddd");
        that.pageselected = true;
    }
    else
    {
        console.log("UNCheckeddddddd");
        that.pageselected = false;
    }
}
      

But this is not behaving as expected. Where am I going wrong?

like image 224
mmt Avatar asked Sep 12 '14 13:09

mmt


People also ask

How can I uncheck checkbox is checked in jQuery?

By using jQuery function prop() you can dynamically add this attribute or if present we can change its value i.e. checked=true to make the checkbox checked and checked=false to mark the checkbox unchecked.

How do you check checkbox is checked or not Onchange jQuery?

To check whether a Checkbox has been checked, in jQuery, you can simply select the element, get its underlying object, instead of the jQuery object ( [0] ) and use the built-in checked property: let isChecked = $('#takenBefore')[0]. checked console. log(isChecked);

Which event is triggered whenever you check a checkbox in jQuery?

The jqxCheckBox checked event is triggered when the checkbox is checked.

How do I uncheck a checkbox in Javascript?

prop() You can use the prop() method to check or uncheck a checkbox, such as on click of a button.


2 Answers

You are checking the row is checked or not, which is not possible.bind onchange event on the checkbox. put it on the checkbox let say checkbox1.

HTML Code:

<input type="checkbox" name="zip" id="checkbox1" class="all"  value="all" />

JS Script:

$('#checkbox1').change(function(){

    if($(this).is(':checked')){
        console.log("CCCCheckeddddddd");
    }
    else
    {
        console.log("UNCheckeddddddd");
    }    

});
like image 69
Shivang MIttal Avatar answered Oct 20 '22 22:10

Shivang MIttal


JSFIDDLE DEMO

Since the checkbox is inside the tr with id othercheckbox .. Use this

$('#othercheckbox input[name="zip"]').click(function () {
    if ($(this).is(':checked')) {
        alert("CCCCheckeddddddd");
        //that.pageselected = true;
    } else {
        alert("UNCheckeddddddd");
        //that.pageselected = false;
    }
});
like image 44
Venkata Krishna Avatar answered Oct 20 '22 20:10

Venkata Krishna