1

$('#checkboxF1').on('change', function() {
  alert("test");
});

function check_box() {
  document.getElementById("checkboxF1").checked = true;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button id="check_box" onclick="check_box();">check box</button>
<br>
<br>
<input type="checkbox" id="checkboxF1" class="css-checkbox">Test

The onchange function works when checking the checkbox directly but it doesn't work when checking the checkbox through a function, i.e. the function named check_box().

I want to invoke the onchange function when check the checkbox is check through the function check_box().

1
  • yea. it too works when i directly click the check box. but it doesn't work when i change the checked state via onclick the button. Commented Dec 31, 2015 at 3:54

5 Answers 5

3

This is normal for input tags. When you change the value programmatically, the change event does not fire. That's just how they are designed.

A usual work-around is to either call the change function directly or trigger the change event manually whenever you change the value programmatically.

Sign up to request clarification or add additional context in comments.

Comments

0

Problem is changing the checkbox state does not fires the event. Try trigger instead.

var chk = $('#checkboxF1');

chk.on('change', function() {
  alert("test");
});

function check_box() {
  chk.trigger('click');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<button id="check_box" onclick="check_box();">check box</button>
<br>
<br>
<input type="checkbox" id="checkboxF1" class="css-checkbox">Test

Comments

0

try this -

// Create a new 'change' event

var event = new Event('change');

// Dispatch it

element.dispatchEvent(event);

let me know if this helps...

Comments

0

See code below.

$('#checkboxF1').on('change', function() {
  alert("test");
});

function check_box() {
  // emulate the checkbox.
  var checkboxF1 = document.getElementById("checkboxF1");
  checkboxF1.checked = !checkboxF1.checked;
  // trigger the onChange event.
  $('#checkboxF1').trigger('change');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button id="check_box" onclick="check_box();">check box</button>
<br>
<br>
<input type="checkbox" id="checkboxF1" class="css-checkbox">Test

Comments

0

Since you're using jQuery, try this:

function check_box(){
    $("#checkboxF1").prop('checked', true);
}

EDIT: I put your function in a snippet, and it works fine too. It checks the box.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<html>
    <head>
        <title>Test</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
    </head>
    <body>

    <button id="check_box" onclick="check_box();"> check box </button>
    <br> <br>
    <input type="checkbox" id="checkboxF1" class="css-checkbox"> Test

    <script>
    $('#checkboxF1').on('change', function() {
  alert("test"); 
});

    function check_box(){
        document.getElementById("checkboxF1").checked = true;   
    }

    </script>

    </body>
    </html>

If you are trying to toggle the checkbox with the button. Then do this:

function check_box(){
    $("#checkboxF1").prop('checked', !$("#checkboxF1").prop('checked'));
}

If this isn't it, then I don't understand the question.

EDIT 2: Ok, if the onchange event doesn't fire; try to force it.

$('#checkboxF1').trigger('change');

3 Comments

yea. it too works when i directly click the check box. but it doesn't work when i change the checked state via onclick the button.
His problem is not that the box doesn't get checked. Its that the onchange handler doesn't fire.
Thanks for translating. Made another edit, though I'm not confident in this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.