Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Open
  • Priority: Low
  • Security: Public
  • Views: 37
  • Last Modified:

How can I check the "checked" status of a checkbox in JQuery?

I've been at this for a while to no avail.

Seems like this should be easy...

Here's what I'm doing:

<!DOCTYPE html>
<html lang="en">
<head>
<title>JQuery Example</title>
<link href="css/stylesheet.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.0.min.js"></script>
</head>

<body>

Spouse <input type="checkbox" id="spouse_check">

<script>
$(document).ready(function() {

	var $checkbox=$('input[id="spouse_check"]');
	if($checkbox).prop('checked'))
	{
		alert("hello");
	}
	
});

</script>
</body>
</html>

Open in new window


Doesn't work. what am I missing?
0
brucegust
Asked:
brucegust
  • 4
  • 2
6 Comments
 
Jayadev NairApplication EngineerCommented:
Are checking this too early? On document ready, do you expect your checkbox will be on checked state?
0
 
brucegustAuthor Commented:
No. I'm looking for something that will check to see if it's checked after the DOM  has loaded and the user has checked the box.
0
 
Jayadev NairApplication EngineerCommented:
So the event you have placed the script is where gone wrong. Register the same in any event that you might want to check, usually at a submit button click as shown below

$("#btnSubmit").button().click(function(){
        var $checkbox=$('input[id="spouse_check"]');
	if($checkbox).prop('checked'))
	{
		alert("hello");
	}
    }); 

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
brucegustAuthor Commented:
I see.

But what about this: https://medium.com/js-dojo/check-if-a-checkbox-is-checked-with-jquery-2843f97d4954

It doesn't seem like he's using a "submit" dynamic. It's all happening when the user clicks on the checkbox. How is he doing that?

I couldn't get it to work...
0
 
Jayadev NairApplication EngineerCommented:
If you cant figure out the event, and if you need this on form submit, try this -

$(document).on('submit','formId',function(){
   // code
});

Open in new window


or

 
$("#formid").on("submit", function(){
   //code
   return true;
 })

Open in new window

0
 
Jayadev NairApplication EngineerCommented:
And if you say the 'checked'  property is not identified then try below script

if ($("#spouse_check").is(":checked")) {
  //alert
}

Open in new window

0

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now