I got a check box on the html page as:
<input type="checkbox" id="chk-info" name="chk-info" />
when I try to write an on change event and run, it's not getting fired. Instead if, I place an alert message before the function it is firing fine.
alert('blah');
var sth = function(){
function m(){
$("input[type='checkbox']").click(function (e) {
if ($(this).is(':checked')) {
alert("true");
} else {
alert("false");
}
});
}
return{ m:m};
};
I have tried using the id selector as well, but with not much luck. Can anyone help me in pointing where the issue is?
EDIT: When I place the check box checked function outside the above function it works well with 'onchange' event attached on the <input>
tag
I got a check box on the html page as:
<input type="checkbox" id="chk-info" name="chk-info" />
when I try to write an on change event and run, it's not getting fired. Instead if, I place an alert message before the function it is firing fine.
alert('blah');
var sth = function(){
function m(){
$("input[type='checkbox']").click(function (e) {
if ($(this).is(':checked')) {
alert("true");
} else {
alert("false");
}
});
}
return{ m:m};
};
I have tried using the id selector as well, but with not much luck. Can anyone help me in pointing where the issue is?
EDIT: When I place the check box checked function outside the above function it works well with 'onchange' event attached on the <input>
tag
- Just tested this and everything is working fine, is this all the code you have ? – doubleOrt Commented Sep 3, 2017 at 1:45
- No, I got other functions in the same page as well and I don't see any errors on the browser console. I got this event placed on the page load as well. – John M Commented Sep 3, 2017 at 1:47
- Could you add the plete Javascript code ? What are you doing to the checkbox ? Is this the only part of the code that interacts with your checkbox ? – doubleOrt Commented Sep 3, 2017 at 1:49
- if the checkbox is checked I need to hide a div content, that's all the JS code that is available. – John M Commented Sep 3, 2017 at 2:08
- Well, the code you have provided works on my puter. So there are no problems with it. I am certain that something is happening in your other code. Go and create a fiddle: jsfiddle. – doubleOrt Commented Sep 3, 2017 at 2:12
2 Answers
Reset to default 5Put the onclick event of the checkbox on page load.
$(document).ready(function () {
$("input[type='checkbox']").click(function (e) {
if ($(this).is(':checked')) {
alert("true");
} else {
alert("false");
}
});
})
<script src="https://ajax.googleapis./ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<input type="checkbox" id="chk-info" name="chk-info" />
<label for="chk-info">label</label>
$(document).ready(function () {
$("input[type='checkbox']").click(function (e) {
if ($(this).is(':checked')) {
$(this).val("true");
} else {
$(this).val("false");
}
});
})
<script src="https://ajax.googleapis./ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<input type="checkbox" id="chk-info" name="chk-info" />
<label for="chk-info">label</label>