简体   繁体   中英

How do I determine if a checkbox is checked?

For some reason, my form does not want to get the value of a checkbox... I am not sure if it is my coding or not, but when I try and alert() the value, I get undefined as a result. What do I have wrong?

<head>
  <script>
    var lfckv = document.getElementById("lifecheck").checked
    function exefunction(){
      alert(lfckv);
    }
  </script>
</head>
<body>
  <label><input id="lifecheck" type="checkbox" >Lives</label>
</body>

EDIT

I tried changing it to this

function exefunction() {
    alert(document.getElementById("lifecheck").checked);
}

But now it doesn't even want to execute . What's going wrong?

Place the var lfckv inside the function. When that line is executed, the body isn't parsed yet and the element "lifecheck" doesn't exist. This works perfectly fine:

 function exefunction() { var lfckv = document.getElementById("lifecheck").checked; alert(lfckv); } 
 <label><input id="lifecheck" type="checkbox" >Lives</label> <button onclick="exefunction()">Check value</button> 

You are trying to read the value of your checkbox before it is loaded. The script runs before the checkbox exists. You need to call your script when the page loads:

<body onload="dosomething()">

Example:

http://jsfiddle.net/jtbowden/6dx6A/

You are also missing a semi-colon after your first assignment.

You can use this code, it can return true or false :

 $(document).ready(function(){ //add selector of your checkbox var status=$('#IdSelector')[0].checked; console.log(status); }); 

The line where you define lfckv is run whenever the browser finds it. When you put it into the head of your document, the browser tries to find lifecheck id before the lifecheck element is created. You must add your script below the lifecheck input in order for your code to work.

 <!doctype html> <html lang="en"> <head> </head> <body> <label><input class="lifecheck" id="lifecheck" type="checkbox" checked >Lives</label> <script type="application/javascript" > lfckv = document.getElementsByClassName("lifecheck"); if (true === lfckv[0].checked) { alert('the checkbox is checked'); } </script> </body> </html> 

so after you can add event in javascript to have dynamical event affected with the checkbox .

thanks

try learning jQuery it's a great place to start with javascript and it really simplifies your code and help separate your js from your html. include the js file from google's CDN (https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js)

then in your script tag (still in the <head> ) use:

$(function() {//code inside this function will run when the document is ready
    alert($('#lifecheck').is(':checked'));

    $('#lifecheck').change(function() {//do something when the user clicks the box
        alert(this.checked);
    });
});

You could try the below to determine checkbox is checked or not.

$('#checkbox_id').is(":checked"); // jQuery

document.getElementById("checkbox_id").checked //JavaScript

this returns true if checkbox is checked

 var elementCheckBox = document.getElementById("IdOfCheckBox"); elementCheckBox[0].checked //return true if checked and false if not checked 

thanks

Following will return true when checkbox is checked and false when not.

$(this).is(":checked")

Replace $(this) with the variable you want to check.

And used in a condition:

if ($(this).is(":checked")) {
  // do something
}
<!DOCTYPE html>
<html>
<body>
    <input type="checkbox" id="isSelected"/>
    <div id="myDiv" style="display:none">Is Checked</div>
</body>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script>
        $('#isSelected').click(function() {
            $("#myDiv").toggle(this.checked);
        });
    </script>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM