367

I am building a web app with jQuery Mobile and want to check if a checkbox is checked:

<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 it doesn't execute it. This is what I have now:

<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>
5
  • 3
    What is remember in this context: if (remember.checked == 1){??? Commented Mar 27, 2012 at 10:05
  • Later on it should remember email and password. It's for a login page Commented Mar 27, 2012 at 10:14
  • What I am trying to say is that remember is undefined in that context. Try console.log(remember);. Commented Mar 27, 2012 at 10:17
  • 2
    As @Steaphann mentioned in the edit, the code did not work because there are two elements with same id in the html. <div id="remember" data-role="fieldcontain"> and <input id="remember" name="remember" type="checkbox" onclick="validate()" />. None of the answers caught this issue. Commented Aug 5, 2020 at 8:08
  • @Steaphann Do not include solution to question please (post a separate answer instead). Commented Jun 12 at 10:14

18 Answers 18

461

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>
Sign up to request clarification or add additional context in comments.

5 Comments

I always get the message 'You didn't check it! Let me check it for you.'
Nope its working fine for me ; i am getting both alert messages.
@Steaphann Did you replace the word 'remember' with your checkbox id? ...oh, just noticed 2012 0.o ...p.s. you forgot the semi-colon after the 2nd alert statement
@josh.thomson semi-colons are optional.
Note to the unwary: don't confuse someElem.checked with someElem.value. someElem.checked returns a boolean telling you if the box is checked or not. someElem.value is the string value that will be sent to the server if the box is checked. Using javascript to get someElem.value will return the same thing ('on', by default) regardless of whether the box is checked.
92

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

2 Comments

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 )
I always get the message 'You didn't check it! Let me check it for you.'
24
//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

Comments

10

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()" />

3 Comments

I always get the message 'You didn't check it! Let me check it for you.'
@user1251632 WFM too. Can you edit your question and show what you have now? Perhaps you made a copy and paste error.
document.getElementById('remember').checked == 1 was changed by me bfore your comment.. it may be the problem.
9

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" />

Comments

9
if (document.getElementById('remember').checked) {
    alert("checked");
}
else {
    alert("You didn't check it! Let me check it for you.");
}

Comments

7

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>

Comments

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

Comments

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("You didn't check it! Let me check it for you.");
    }

  }
</script>

Comments

5

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!

Comments

5

This should allow you to determine if an element with id='remember' is 'checked':

if (document.getElementById('remember').is(':checked'))

3 Comments

is does not belong to the DOM element object
.is() - is a jQuery function. .checked is javascript.
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
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.")
    }
}

Comments

2

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>

1 Comment

I recommend you to use attr onclick or use jQuery code.
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.");
  }
}

Comments

1

You can try this:

if ($('#remember').is(':checked')){
   alert('checked');
}else{
   alert('not checked')
}

Comments

-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>

Comments

-1

You can also use JQuery methods to accomplish this:

<script type="text/javascript">
if ($('#remember')[0].checked) 
{
 alert("checked");
}
</script>

Comments

-4

The below will definitely work. updated 2022

<input onchange="isChecked()" type="checkbox" required="required"/>

<script type="text/javascript">
        let ischeckvariable = false;
    function isChecked() {
        if (!ischeckvariable) {
            ischeckvariable = true;
        } else {
            isckeckvariable = false;
        }
    }
</script>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.