Skip to content
Advertisement

How to implement a click event on mutually exclusive checkboxes in jQuery?

I have two checkboxes:

JavaScript

I’ve made them mutually exclusive with:

JavaScript

This works fine. But I also want to add additional click functionality to the ‘inside’ checkbox. I want it to enable/disable a textarea on the same form, so I’ve done this:

JavaScript

So, if the ‘inside’ checkbox is checked, the textarea will be enabled, if it’s not checked, the textarea should be disabled.

The problem is that if the ‘inside’ checkbox is checked, and the user then checks the ‘outside’ checkbox, the ‘inside’ becomes unchecked (as it should be), but the textarea remains enabled. This appears to be because the ‘inside’ checkbox was never actually clicked by the user.

I’ve tried working around this with the following:

JavaScript

But this just throws the browser in a loop, since the two different click events end up calling each other.

How can I keep my mutually exclusive code while still allowing the enable/disable code for the ‘inside’ checkbox to work?

Advertisement

Answer

You can create a custom event that you trigger when you click on #application_inside. Also you will fire this custom event when you uncheck other boxes because of the exclusiveness.

Here is an example: http://jsfiddle.net/gs78t/2/

Advertisement