On a specific condition when user checks a checkbox, I want to show an alert message and uncheck the checkbox, so to uncheck a checkbox I am calling click function on it so that it internally unchecks and also fire necessary events.
I have a checkbox and label which is for checkbox, with for
property properly set for the label
- In firefox, unchecking works on checkbox and it's label.
- In chrome, unchecking works on checkbox for but not on label.
- In chrome, unchecking doesn't work on checkbox or label.
With specific timeout I was able to solve this problem, but I want a solution without using timeout.
You can see code below and also access jsfiddle here.
$("#id1").on("change", function(event) {
if (event.target.checked) {
$(event.target).trigger("click");
}
});
$("#id2").on("change", function(event) {
setTimeout(function() {
if (event.target.checked) {
$(event.target).trigger("click");
}
}, 5);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<input type="checkbox" value="" id="id1">
<label for="id1">Click me</label>
<br/>
<input type="checkbox" value="" id="id2">
<label for="id2">Works with timeout</label>
This work quite well with Firefox but not in chrome and IE9, IE10
I am looking for javascript or jQuery solution