Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery checking a checkbox and triggering javascript onclick event

I'm trying to check a checkbox using jQuery and trigger the onclick event in the process.

Say I have a checkbox defined in html:

<input type="checkbox" value="checked" name="check1" onclick="alert(this.value);"> 

And I have a jQuery statement that is triggered in a function:

$('input[name=check1]').attr('checked', true); 

The result is the checkbox gets checked but the javascript onclick event is not triggered (hence no alert). But if I were to trigger the click even manually as such:

$('input[name=check1]').attr('checked', true).trigger('click'); 

The result is the checkbox gets checked, javascript onclick event is triggered (and the value is correctly gotten) but then the checkbox gets unchecked after that.

Can anyone tell me how I can achieve what I'm trying to do?

like image 504
YK Tan Avatar asked Apr 22 '12 13:04

YK Tan


People also ask

How do I trigger a click event in a checkbox?

To trigger a checkbox click event even if it's checked with jQuery, we can call the jQuery trigger method with 'click' . Then we can cal trigger with 'click' by writing: $('input').

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

appears trigger('click') toggles the current value. If you want to just set it then set checked to true then call triggerHandler(...)

Can we use Onclick for checkbox?

The CheckBox has been assigned a JavaScript OnClick event handler. When the CheckBox is clicked, the ShowHideDiv JavaScript function is executed. Inside this function, based on whether CheckBox is checked (selected) or unchecked (unselected), the HTML DIV with TextBox is shown or hidden.

How to bind to HTML checkbox change or click event with jQuery?

There are several ways to bind to HTML checkbox change or click event with JavaScript and jQuery. 1. Using jQuery With jQuery, you can use the .is () method, which matches the contents of a jQuery object against a selector. The following code demonstrates this with the :checked CSS pseudo-class selector.

How to check a checkbox dynamically with jQuery?

How to check a checkbox with jQuery? There are two methods by which you can dynamically check the currently selected checkbox by changing the checked property of the input type. Method 1: Using the prop method: The input can be accessed and its property can be set by using the prop method. This method manipulates the ‘checked’ property ...

How to check and uncheck checkbox using JavaScript?

For checking and unchecking a checkbox, you can either use JavaScript or jQuery methods described below. Use the following code to check and uncheck checkbox using JavaScript: jQuery provides the attr ( ) and prop () methods to accomplish the task. The choice depends on the jQuery versions.

How to check input in jQuery?

Method 2: Using the attr method: It is similar to the above method and more suitable for older jQuery versions. The input can be accessed and its property can be set by using the attr method. We have to manipulate the ‘checked’ property and set it to true or false depending on whether we want to check or uncheck it.


2 Answers

Use .triggerHandler() instead of .trigger():

$('input[name=check1]').attr('checked', true).triggerHandler('click'); 

Also, use .prop() instead of .attr():

$('input[name=check1]').prop('checked', true).triggerHandler('click'); 

(if you're using jQuery 1.6 or newer.) edit — Also, as I commented on another answer, you have to watch out for jQuery's weird behavior when programmatically triggering events. Here is an illustrative jsfiddle. When a real "click" happens on an element, the "click" handler for the element will see the updated value of the "checked" flag. That is, if you click on an unchecked checkbox, the click handler will see the "checked" flag set to true. However, if you trigger "click" on an unchecked checkbox via jQuery, the "click" handler will see the "checked" flag set to false! That's a really bad thing, in my opinion, but it's always done that.

edit again — oh, also, I forgot another important (and irritating) jQuery weirdness: for reasons unknown, the .triggerHandler() API will only invoke handlers on the first matched element. If you try to trigger all the checkbox "click" handlers, in other words:

$('input:checkbox').triggerHandler('click'); 

then only the first checkbox on the page will be triggered. What I generally do in order to deal with the insanity is bind the handler with my own fake event name as well as "click":

$('input:checkbox').bind('click my-click', function() ... ) // or ".on()" with 1.7 

That way I can trigger "my-click" and get the best of both worlds: the library triggers the handler on all the matched elements, but it won't toggle the actual state of the elements.

like image 162
Pointy Avatar answered Oct 03 '22 20:10

Pointy


Change:

$('input[name=check1]').attr('checked', true).trigger('click'); 

To:

$('input[name=check1]').trigger('click').prop('checked', true); 
like image 34
iambriansreed Avatar answered Oct 03 '22 19:10

iambriansreed