How to trigger checkbox click event even if it's checked through Javascript code?

JavascriptJqueryCheckboxDom Events

Javascript Problem Overview


I have many checkboxes in my page and there is a select all checkbox which checks all the checkboxes. Somehow I want to emulate that click event of checkbox even if it's checked/unchecked through select all button. How can I do it?

Javascript Solutions


Solution 1 - Javascript

You can use the jQuery .trigger() method. See http://api.jquery.com/trigger/

E.g.:

$('#foo').trigger('click');

Solution 2 - Javascript

Getting check status

var checked = $("#selectall").is(":checked");

Then for setting

$("input:checkbox").attr("checked",checked);

Solution 3 - Javascript

You can use .change() function too

E.g.:

$('form input[type=checkbox]').change(function() { console.log('hello') });

Solution 4 - Javascript

You can also use this, I hope you can serve them.

$(function(){
  $('#elements input[type="checkbox"]').prop("checked", true).trigger("change");
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>

<div id="elements">
  <input type="checkbox" id="item-1" value="1"> Item 1 <br />
  <input type="checkbox" id="item-2" value="2" disabled> Item 2 <br /> 
  <input type="checkbox" id="item-3" value="3" disabled> Item 3 <br />
  <input type="checkbox" id="item-4" value="4" disabled> Item 4 <br />
  <input type="checkbox" id="item-5" value="5"> Item 5
</div>

Solution 5 - Javascript

no gQuery

document.getElementById('your_box').onclick();

I used certain class on my checkboxes.

var x = document.getElementsByClassName("box_class");
var i;
for (i = 0; i < x.length; i++) {
    if(x[i].checked) x[i].checked = false;
    else x[i].checked = true;
    x[i].onclick();
   }

Solution 6 - Javascript

Trigger function from jQuery could be your answer.

[jQuery docs says:][1] Any event handlers attached with .on() or one of its shortcut methods are triggered when the corresponding event occurs. They can be fired manually, however, with the .trigger() method. A call to .trigger() executes the handlers in the same order they would be if the event were triggered naturally by the user

Thus best one line solution should be:

$('.selector_class').trigger('click');

//or

$('#foo').click();

[1]: http://api.jquery.com/trigger/ "jQuery docs"

Solution 7 - Javascript

  $("#gst_show>input").change(function(){

    var checked = $(this).is(":checked");
    if($("#gst_show>input:checkbox").attr("checked",checked)){
      alert('Checked Successfully');
    }

  });

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJishnu A PView Question on Stackoverflow
Solution 1 - JavascriptMark RobinsonView Answer on Stackoverflow
Solution 2 - JavascriptHarishView Answer on Stackoverflow
Solution 3 - JavascriptsilviomoretoView Answer on Stackoverflow
Solution 4 - JavascriptLearning and sharingView Answer on Stackoverflow
Solution 5 - JavascriptHarijs KrūtainisView Answer on Stackoverflow
Solution 6 - Javascriptuser4466709View Answer on Stackoverflow
Solution 7 - JavascriptRaj ShekharView Answer on Stackoverflow