Question

I want to check whether checkbox is checked or not using jquery. I want to check it on checkbox's onclick event.

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

Is it possible? How?

Thanks.

Was it helpful?

Solution

First, don't use javascript: in event handler attributes. It's wrong and only works because it happens to be valid JavaScript syntax. Second, your id is not valid. Parentheses are not allowed in the id attribute (in HTML 4 at least; HTML 5 lifts this restriction). Third, if you're using jQuery it probably makes sense to use its click() method to handle the click event, although be aware that changing it to do that will mean that if the user clicks on the checkbox before the document has loaded then your script won't handle 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!");
        }
    });
});

OTHER TIPS

You can also use this

if($("#checkbox_id").is(':checked'))

You can do like this

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

Or using is() method:

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

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

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

Make sure to wrap your code in ready handler:

$(function(){
  // code....
});

There are Several options are there like....

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

Firstly, bind the event in script tags, rather than inline. This is much easier to follow and makes your HTML far more readable. Then you can use the jQuery selector :checked to determine whether the checkbox is checked. Then you can use the checked attribute of the element.

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

Try This:

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

You can do this using is() method:

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

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

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

Make sure to wrap your code in document ready handler:

 $(document).ready(function() {
      // code....
  });

Here is another example, notice instead of using a onClick event in the Element itself i would prefer to use a listener, but remove the brackets from the id, It will give you a hard 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>

html

 <input type="checkbox" id="chkBox" checked/> Change Check Box

jquery

$("#chkBox").change(function() {
     if (this.checked) {
         alert("Checked");
         }
         else {
             alert("Not Checked")
             }
           });

 $("#chkBox").change(function() {
 if (this.checked) {
     alert("Checked");
     }
     else {
         alert("Not Checked")
         }
       });
    
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" id="chkBox" checked/> Change Check Box

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top