Check if checkbox checked or not with jquery

I want to check if the checkbox is checked or not using jquery. I want to check this on the onclick event flag.

<input type="checkbox" onclick="javascript:check_action();" id="Public(Web)" checked="checked" value="anyone" name="data[anyone]"> 

Is it possible? How?

Thanks.

+7
javascript jquery html checkbox checked
source share
8 answers

First, do not use javascript: in event handler attributes. It is erroneous and only works because it is valid JavaScript syntax. Secondly, your id invalid. Brackets are not allowed in the id attribute (at least in HTML 4, HTML 5 removes this restriction). Thirdly, if you use jQuery, it probably makes sense to use your click() method to handle the click event, although keep in mind that changing it for this will mean that if the user clicks on the checkbox before the document is your The script will not process it.

 <input type="checkbox" id="Public_Web" checked value="anyone" name="data[anyone]"> $(document).ready(function() { $("#Public_Web").click(function() { if (this.checked) { alert("Checked!"); } }); }); 
+8
source share

You can also use this

 if($("#checkbox_id").is(':checked')) 
+8
source share

You can do it as follows:

 $('#checkbox_id').click(function(){ alert(this.checked); }); 

Or using the is() method:

 $('#checkbox_id').click(function(){ if ($(this).is(':checked')){ alert('Checked'); } else{ alert('Not Checked'); } }); 

If you want to do this for all the :checkbox inside the form, you can use the filter selector :checkbox as follows:

 $('#form_id :checkbox').click(function(){ alert(this.checked); }); 

Be sure to wrap your code in a ready-made handler:

 $(function(){ // code.... }); 
+4
source share

There are several options, for example ....

 1. if($("#ans").is('checked')) 2. if($("#ans:checked")) 3. if($('input:checkbox:checked')) 
+2
source share

First, bind the event in script tags, not in a string. This is much simpler and your HTML is much more readable. Then you can use the jQuery :checked selector to determine if the checkbox is checked. Then you can use the checked attribute of this element.

 $(document).ready(function(){ $('#Public(Web)').click(function(){ if (this.checked) { // do your code here } }); }); 
+1
source share

Try the following:

 if(!$('#elementid').is(':checked')){ alert('Not checked'); }else{ alert('checked'); } 
+1
source share

You can do this using the is() method:

 $('##Public_Web').click(function(){ if ($(this).is(':checked')){ alert('Checked'); } else{ alert('Not Checked'); } }); 

If you want to do this for all the :checkbox inside the form, you can use the filter selector :checkbox :

 $('#form_id :checkbox').click(function(){ alert(this.checked); }); 

Be sure to wrap your code in a document handler:

  $(document).ready(function() { // code.... }); 
+1
source share

Here is another example: a notification instead of using the onClick event in the element itself, I would prefer to use a listener, but remove the parentheses from id, this will complicate your time in some browsers.

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html><head> <input type="checkbox" id="PublicWeb" checked="checked" value="anyone" name="data[anyone]" /> <script type='text/javascript' src='http://code.jquery.com/jquery-latest.min.js'></script> <script> $(document).ready(function(){ $('#PublicWeb').click(function(){ if($(this).is(':checked')){ alert("its checked alright"); } }); }); </script> </head><body></body></html> 
0
source share

All Articles