0

There are many solutions of change event through click, but not programatically.

See the fiddle here http://jsfiddle.net/pratbhoir/ma7rk0ur/2/

JS Code

$(document).ready(function () {
    //set initial state.
    $('#textbox1').val($(this).is(':checked'));

    //This Event is not called when toggled by button
    $('#myCheckBox').change(function () { //-------------if(this.checked
        if ($(this).is(":checked")) {
            var returnVal = confirm("Are you sure?");
            $(this).attr("checked", returnVal);
        }
        $('#textbox1').val($(this).is(':checked'));
    });
});

I want to call the change event, when checkbox event is fired by toggle button.

P.S. Please dont say to put a event on toggle button.

Thank you and have a nice day.

Pratik Bhoir
  • 2,074
  • 7
  • 19
  • 36
  • can u please, try it in fiddle and show me – Pratik Bhoir Sep 04 '14 at 06:11
  • sorry... `$('#myCheckBox').prop('checked', false).change();` – Arun P Johny Sep 04 '14 at 06:11
  • http://jsfiddle.net/arunpjohny/ma7rk0ur/4/ – Arun P Johny Sep 04 '14 at 06:13
  • Hi pratik, Will tigger function will work here, I am asking this because you do not want do to any programing to fire the even, but the event you have attached to checkbox only when some one click on it, if you want to fire this event on toggle button click you have to write $('#myCheckBox').trigger('change'); in the function toggleCheckBox – Harikant Sep 04 '14 at 06:21

1 Answers1

0

Call change explicitly

function toggleCheckBox() {
    var $myCheckBox=$('#myCheckBox');//better store ref into local variable
    if ($myCheckBox.is(":checked")) {
        $myCheckBox.prop('checked', false)
    } else {
        $myCheckBox.prop('checked', true);
    }
    $myCheckBox.change();//trigger change 
}

Here's the demo http://jsfiddle.net/vikrant47/ma7rk0ur/7/

vikrant singh
  • 2,091
  • 1
  • 12
  • 16