Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check if a checkbox is checked?

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'

like image 305
Steaphann Avatar asked Mar 27 '12 10:03

Steaphann


People also ask

Which method is used to determine if a checkbox is checked?

Verify if a checkbox is checked or not In order to check if a checkbox is checked or unchecked, we can used the isSelected() method over the checkbox element. The isSelected() method returns a boolean value of true if the checkbox is checked false otherwise.

How do you check if a checkbox is checked or not in react?

Use the target. checked property on the event object to check if a checkbox is checked in React, e.g. if (event. target. checked) {} .


1 Answers

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> 
like image 73
Pranav Avatar answered Sep 21 '22 22:09

Pranav