Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 866
Jishnu A P Avatar asked Jan 24 '11 10:01

Jishnu A P


People also ask

How do you check if a checkbox is already checked in Javascript?

Checking if a checkbox is checked First, select the checkbox using a DOM method such as getElementById() or querySelector() . Then, access the checked property of the checkbox element. If its checked property is true , then the checkbox is checked; otherwise, it is not.

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

When a user click on a checkbox with a click listener, it first flips the "checked" state and then fires the event. When a developer initiates a click on a checkbox with jQuery, it first fires the event and then flips the "checked" state.

How do you trigger an event on button click?

A single click event bind to a button with an Id of “button2”. and a trigger to execute the button1 click event handler. $("#button2"). bind("click", (function () { alert("Button 2 is clicked!"); $("#button1").


2 Answers

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

E.g.:

$('#foo').trigger('click'); 
like image 189
Mark Robinson Avatar answered Sep 17 '22 18:09

Mark Robinson


Getting check status

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

Then for setting

$("input:checkbox").attr("checked",checked); 
like image 31
Harish Avatar answered Sep 21 '22 18:09

Harish