1

i have two checboxes

<input type="checkbox" name="some" >
<input type="checkbox" name="other" >

the thing i want to do that when i check one of them other one should automatically unchecked. Remember that name of both checkboxes are changed not same . please help me . Thanx in advance

1
  • Why don't you use radio buttons for this? (<input type="radio" name="other" >) Commented Dec 15, 2011 at 8:37

5 Answers 5

1
$('input[name="some"]').click(function(){
    $('input[name="other"]').removeAttr('checked');
});

$('input[name="other"]').click(function(){
    $('input[name="some"]').removeAttr('checked');
});

Working sample

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.js"></script>
<input type="checkbox" name="some" >
<input type="checkbox" name="other" >

<script>
$('input[name="some"]').click(function(){
    $('input[name="other"]').removeAttr('checked');
});

$('input[name="other"]').click(function(){
    $('input[name="some"]').removeAttr('checked');
});

</script>
Sign up to request clarification or add additional context in comments.

1 Comment

Very strange, it works for me. Editing answer with working sample.
0

Try to work with radio buttons, because so you don't have to add any javascript code !
Radio buttons only allow one selection.

Comments

0

I think it would be a good choice to use classes on the checkboxes and from there and then just call the functions.

1 Comment

i didn't get you . what you want to say ?
0

If you are using jQuery v1.6 or higher;

$('input[name="some"]').click(function(){
    $('input[name="other"]').prop("checked", false);
});

This code unchecks the 'other' checkbox when the 'some' checkbox is clicked.

3 Comments

can i use this $("#some") instead of $('input[name="some"]')
@Ahsan Attari: yes if you have an ID value of some then its just fine.
no, its still not working i tried .attr("checked",false) .prop("checked",false) .removeAttr("Checked") but its not working
0
$("input[type=checkbox]").click(function(){

  $('input[type=checkbox]').each(function () {
      $(this).prop("checked", false);
  }
   $(this).prop("checked", true);

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.