How can I check if a checkbox is checked?

0 votes

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.

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>
Oct 8 in Java-Script by kartik
• 35,820 points
27 views

1 answer to this question.

0 votes

Hello @kartik,

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

Hope it works!!

Thank YOU!!

answered Oct 8 by Niroj
• 78,120 points

Related Questions In Java-Script

0 votes
1 answer

How do I check if an element is hidden in jQuery?

Hello @kartik,  You can use CSS: class .hide { ...READ MORE

answered Jun 8 in Java-Script by Niroj
• 78,120 points
70 views
0 votes
1 answer

How to check if a specific key is present in a hash or not?

Hello @kartik, While Hash#has_key? gets the job done, as it has ...READ MORE

answered Jul 24 in Java-Script by Niroj
• 78,120 points
230 views
0 votes
1 answer

How do I check if an HTML element is empty using jQuery?

Hello @kartik, Try: if ($('#element').is(':empty')){ //do something }  If you ...READ MORE

answered Aug 25 in Java-Script by Niroj
• 78,120 points
172 views
0 votes
1 answer

How can I send a message to a particular client with socket.io?

Hii, You can try the code below: io.to(socket.id).emit("event", data); whenever ...READ MORE

answered Apr 27 in Java-Script by Niroj
• 78,120 points
5,845 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18 in Laravel by Niroj
• 78,120 points
3,506 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18 in Laravel by Niroj
• 78,120 points
418 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23 in Laravel by Niroj
• 78,120 points
287 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23 in Laravel by Niroj
• 78,120 points
4,203 views
0 votes
1 answer

How can I determine if a variable is 'undefined' or 'null'?

Hello @kartik, You can use the qualities of ...READ MORE

answered Aug 28 in Java-Script by Niroj
• 78,120 points
44 views
0 votes
1 answer

How can I check whether a radio button is selected with JavaScript?

Hello @kartik, You can use vanilla JavaScript way var radios ...READ MORE

answered Sep 3 in Java-Script by Niroj
• 78,120 points
76 views