[jQuery] how to uncheck the check box
<div>Thanks for your response ,</div><div>
</div><div>In my application i dont want default checked = checked , </div><div>
</div><div>
</div><div>Then how i do Peter ,</div><div>
</div><div>Thanks</div><div>
</div>There are a few ways to do this.
You can select by class, in which case you give a group of checkboxes the same class
$(function(){
// attach a click event to each checkbox whose class is 'onechecked'
$(':checkbox.onechecked').click(function(){
// if the clicked checkbox is checked (no need to do anything if it is being unchecked)
if (this.checked) {
// loop through all other checkboxes with the same class
$(':checkbox.onechecked').each(function(){
// uncheck all of them
this.checked = false;
});
// re-check the one clicked
this.checked = true;
}
});
});
HTML:
<fieldset>
<input type="checkbox" checked="checked" name="cb1" class="onechecked" />
<input type="checkbox" name="cb2" class="onechecked" />
<input type="checkbox" name="cb3" class="onechecked" />
<input type="checkbox" name="cb4" class="onechecked" />
</fieldset>
you can also use the name attribute of the checkbox to group them - this could work for multiple groups of checkboxes - this example assumes that each group of checkboxes has the same name attribute, and those you want to limit to one selection are prefixed 'limit'
$(function(){
// attach a click event to each checkbox whose name begins with 'limit'
$(':checkbox[name^=limit]').click(function(){
// store the name of the checkbox which has been clicked
var inputname = $(this).attr("name");
// if the clicked checkbox is checked (no need to do anything if it is being unchecked)
if (this.checked) {
// loop through all other checkboxes with the same name attribute as the one clicked
$(':checkbox[name='+inputname+']').each(function(){
// uncheck all of them
this.checked = false;
});
// re-check the one clicked
this.checked = true;
}
});
});
HTML:
<fieldset>
<input type="checkbox" checked="checked" name="limit1" />
<input type="checkbox" name="limit1" />
<input type="checkbox" name="limit1" />
<input type="checkbox" name="limit1" />
</fieldset>
<fieldset>
<input type="checkbox" checked="checked" name="limit2" />
<input type="checkbox" name="limit2" />
<input type="checkbox" name="limit2" />
<input type="checkbox" name="limit2" />
</fieldset>
There are other ways as well, but this should give you some idea about some of the possibilities.
on 15/05/2009 15:48 elubin said::
> add a click handler to each checkbox (you could use a selector and
> loop through with $.each). when clicked, the function could use the
> same selected and loop through again making sure the others are off.
>
> Eric
>
>
>
> On May 15, 10:38 am, bharani kumar <<a href="mailto:bharanikumariyer...@gmail.com">bharanikumariyer...@gmail.com</a>>
>