285

I am building a mobile web app with jQuery Mobile and I want to check if a checkbox is checked. Here is my code.

<script type=text/javascript>
  function validate(){
    if (remember.checked == 1){
      alert("checked") ;
    } else {
      alert("You didn't check it! Let me check it for you.")
    }
  }
</script>

<input id="remember" name="remember" type="checkbox" onclick="validate()" />

But for some reason or another it doesn't execute it.

Please help !

----EDIT----- This is what I have for the moment.

<DIV data-role="content" data-theme="g">
    <DIV class=ui-grid-g-login>
        <FORM method=post action=[$=PROBE(266)/] data-theme="C">
            <P>~DATA_ERROR~</P>
            <div id="mail" data-role="fieldcontain">
                <label for="mail">Email:*</label>       
                <input id="mail" name="mail" type="email" />
            </div>
            <div id="pass" data-role="fieldcontain">
                <label for="pass">Paswoord:*</label>        
                <input id="pass" name="pass" type="password" />
            </div>
            <div id="remember" data-role="fieldcontain">
                <label for="remember">Onthoud mij</label>       
                <input id="remember" name="remember" type="checkbox" onclick="validate()" />
            </div>
            <P><INPUT class=btn name=submit value=Login type=submit  onclick="validate()"></P>  
        </FORM>
    </DIV>
</DIV><!-- /content -->

<script type=text/javascript>
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.")
    }
}
</script>

----EDIT--

Solved it, the problem was that the fieldcontain was also named 'remember'

T.J. Crowder
  • 959,406
  • 173
  • 1,780
  • 1,769
Steaphann
  • 2,931
  • 3
  • 17
  • 15

17 Answers17

380

checked is a boolean property, so you can directly use it in an if condition

<script type="text/javascript">
    function validate() {
        if (document.getElementById('remember').checked) {
            alert("checked");
        } else {
            alert("You didn't check it! Let me check it for you.");
        }
    }
</script>
Agostino
  • 2,435
  • 7
  • 41
  • 64
Pranav
  • 8,013
  • 4
  • 24
  • 41
83

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().

Newbyte
  • 1,716
  • 4
  • 22
  • 39
clem
  • 3,235
  • 1
  • 21
  • 33
  • 1
    You should also advice to strip that `== 1`. Its only working because of two errors. Actually the value is `true` or `false`, he should just using `if( remeber.checked )` – jAndy Mar 27 '12 at 10:08
  • I always get the message 'You didn't check it! Let me check it for you.' – Steaphann Mar 27 '12 at 10:15
13

This should allow you to check if element with id='remember' is 'checked'

if (document.getElementById('remember').is(':checked')
Matas Vaitkevicius
  • 54,146
  • 29
  • 227
  • 241
Peter
  • 219
  • 2
  • 2
  • 4
    `is` does not belong to the DOM element object – yves amsellem Jul 19 '15 at 15:48
  • 19
    `.is()` - is a jQuery function. `.checked` is javascript. – max kaplan Nov 02 '15 at 22:30
  • 4
    Why does this have so many upvotes? Between the missing parenthesis and the mix between vanilla and jquery. The answer is terrible It's either `$('#remember').is(':checked')` or `document.getElementById('remember').checked` but mixing will never work – Tofandel Jun 08 '21 at 18:20
8

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" />
Luke
  • 11,400
  • 43
  • 61
  • 68
8
//HTML
<input type="checkbox" id="someID">
// JavaScript
const someCheckbox = document.getElementById('someID');

someCheckbox.addEventListener('change', e => {
  if(e.target.checked === true) {
    console.log("Checkbox is checked - boolean value: ", e.target.checked)
  }
if(e.target.checked === false) {
    console.log("Checkbox is not checked - boolean value: ", e.target.checked)
  }
});

know more

Crave
  • 165
  • 2
  • 14
MD SHAYON
  • 7,911
  • 47
  • 35
7

use like this

<script type=text/javascript>
    function validate(){
        if (document.getElementById('remember').checked){
            alert("checked") ;
        } else {
            alert("You didn't check it! Let me check it for you.")
        }
    }
</script>

<input id="remember" name="remember" type="checkbox" onclick="validate()" />
Douwe de Haan
  • 5,424
  • 1
  • 28
  • 40
hkutluay
  • 6,614
  • 2
  • 30
  • 51
6

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("Not checked.");
    }

  }
</script>
Douwe de Haan
  • 5,424
  • 1
  • 28
  • 40
Divyesh
  • 81
  • 1
  • 7
6

This should work

function validate() {
    if ($('#remeber').is(':checked')) {
        alert("checked");
    } else {
        alert("You didn't check it! Let me check it for you.");
    }
}
Douwe de Haan
  • 5,424
  • 1
  • 28
  • 40
Aditya
  • 113
  • 2
  • 11
4
if (document.getElementById('remember').checked) {
    alert("checked");
}
else {
    alert("You didn't check it! Let me check it for you.");
}
Douwe de Haan
  • 5,424
  • 1
  • 28
  • 40
Lakshmana Kumar
  • 1,159
  • 3
  • 15
  • 34
4

I am using this and it works for me with Jquery:

Jquery:

var checkbox = $('[name="remember"]');

if (checkbox.is(':checked'))
{
    console.log('The checkbox is checked');
}else
{
    console.log('The checkbox is not checked');
}

Is very simple, but work's.

Regards!

Radames E. Hernandez
  • 3,819
  • 24
  • 33
3

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.")
    }
}
Quentin
  • 857,932
  • 118
  • 1,152
  • 1,264
1

The remember variable is undefined. so try this way:

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.");
  }
}
1

You can use this simple and effective code using pure JS and jQuery.

using validate function as onclick attr

function validate(el) {
    if (el.checked) {
        alert("checked")
    } else {
        alert("unchecked")
    }
}
<input id="remember" name="remember" type="checkbox" onclick="validate(this)" />

OR

using pure JS

document.addEventListener("DOMContentLoaded", function(event) { 
    //using pure Js code
    let chk = document.getElementById("remember");
    if (chk.checked) {
        alert("checked")
    }else{
        alert("unchecked")
    }
})
<input id="remember" name="remember" type="checkbox"/>

using jQuery

$(document).ready(function () {
    //using jQuery code
    $('#remember').on('change', function (e) {
        if (e.currentTarget.checked) {
            alert("checked")
        } else {
            alert("unchecked")
        }
    })
})
<input id="remember" name="remember" type="checkbox"/>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
Dhiaa Shalabi
  • 1,040
  • 8
  • 19
0

You can try this:

if ($('#remember').is(':checked')){
   alert('checked');
}else{
   alert('not checked')
}
Radames E. Hernandez
  • 3,819
  • 24
  • 33
0

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>
Douwe de Haan
  • 5,424
  • 1
  • 28
  • 40
Divyesh
  • 81
  • 1
  • 7
-1

Try This

<script type="text/javascript">
window.onload = function () {
    var input = document.querySelector('input[type=checkbox]');

    function check() {
        if (input.checked) {
            alert("checked");
        } else {
            alert("You didn't check it.");
        }
    }
    input.onchange = check;
    check();
}
</script>
-2

You can also use JQuery methods to accomplish this:

<script type="text/javascript">
if ($('#remember')[0].checked) 
{
 alert("checked");
}
</script>
Jesse
  • 3,334
  • 6
  • 25
  • 38
Supriya
  • 1
  • 1