I am building a mobile web app with jQuery Mobile and I want to check if a checkbox is checked. Here is my code.
Use this below simple code: https://jsfiddle.net/Divyesh_Patel/v7a4h3kr/7/
<input type="checkbox" id="check">
<a href="#" onclick="check()">click</a>
<button onclick="check()">
button
</button>
<script>
function check() {
if (document.getElementById('check').checked) {
alert("checked");
} else {
alert("You didn't check it! Let me check it for you.");
}
}
</script>
If you are using this form for mobile app then you may use the required attribute html5. you dont want to use any java script validation for this. It should work
<input id="remember" name="remember" type="checkbox" required="required" />
Try this:
function validate() {
var remember = document.getElementById("remember");
if (remember.checked) {
alert("checked");
} else {
alert("You didn't check it! Let me check it for you.");
}
}
Your script doesn't know what the variable remember
is. You need to get the element first using getElementById().
You can try this:
if ($(#remember).is(':checked')){
alert('checked');
}else{
alert('not checked')
}
This should allow you to check if element with id='remember'
is 'checked'
if (document.getElementById('remember').is(':checked')
remember
is undefined … and the checked
property is a boolean not a number.
function validate(){
var remember = document.getElementById('remember');
if (remember.checked){
alert("checked") ;
}else{
alert("You didn't check it! Let me check it for you.")
}
}