-4

Does prop("checked", false) reset the <option> value on checkbox? For example:

<input type="checkbox" name="price_range" value="1" id="ceo"> < $10,000
<input type="checkbox" name="price_range" value="2" id="bod"> < $ 200,000.00

Let say if I have two checkbox, when I click on id=ceo, then, I click on id=bod it will overwrite the value of the checkbox. Can I use coding as below to get the value:

if ($('#ceo').is(":checked"))
{
    //if ceo is check           
} 
else if ($('#bod').is(":checked")) 
{
    //if bod is check
} 
else 
{
   //set prop("checked", false)
}
6
  • Use this: stackoverflow.com/questions/2279760/… Commented Sep 19, 2016 at 9:39
  • 3
    "Does prop("checked", false) reset the <option> value on checkbox?" Checkboxes have no <option> value. What are you really asking? prop("checked", false) makes the checkbox not checked. That's it. Commented Sep 19, 2016 at 9:39
  • 2
    It sounds as though your checkboxes should be radio buttons. Commented Sep 19, 2016 at 9:40
  • "Does prop("checked", false) reset the <option> value on checkbox" - No. It changes the "checked" property on the checkbox, nothing else. Commented Sep 19, 2016 at 9:43
  • This may help you ex. stackoverflow.com/questions/5839884/… Commented Sep 19, 2016 at 9:45

5 Answers 5

2

Just use attribute selctor in jquery

$("input[name='price_range']").click(function() {
  $("input[name='price_range']").removeProp('checked');
  $(this).prop('checked', true);
  console.log(this.value);
});
Sign up to request clarification or add additional context in comments.

1 Comment

I have tried yours and it work great. Thank you @SudharsanS. Can you briefly explain what does the .removeProp('checked') for. I'm unclear with that.
2

.prop("checked", false) makes the checkbox not checked. That's it. It has no effect o the checkbox's value (if that's what you meant by "<option> value").

It sounds as though your checkboxes should be radio buttons, since users don't expect checkboxes to behave like radio buttons, and surprises make for poor UX.

But if it's important that they be checkboxes, but also be mututally-exclusive, you can implement that by searching for others with the same name and unchecking them:

$("input[name=price_range]").on("click", function() {
  if (this.checked) {
    $("input[name=" + this.name + "]").not(this).prop("checked", false);
  }
});

Example:

$("input[name=price_range]").on("click", function() {
  if (this.checked) {
    $("input[name=" + this.name + "]").not(this).prop("checked", false);
  }
});
<label>
  <input type="checkbox" name="price_range" value="1" id="ceo">&lt; $10,000</label>
<label>
  <input type="checkbox" name="price_range" value="2" id="bod">&gt; $ 200,000.00</label>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

(See also the use of label in the snippet.)

Comments

0

I suppose you want to have at most one checkbox checked at anytime, right? If that is the case, you may checkout out radio input.

Comments

0

You should use radio button instead of checkbox here

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" name="price_range" value="1" id="ceo"> 
<input type="radio" name="price_range" value="2" id="bod">

Checkbox should be used when you want to select multiple values. If however you wish to implement it using checkbox then

$('input:checkbox').click(function() {
    $('input:checkbox').not(this).prop('checked', false);
});  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="price_range" value="1" id="ceo"> 
<input type="checkbox" name="price_range" value="2" id="bod">

Comments

0
$("input[name=price_range]").on("click", function() {
    if (this.checked && this.id === 'ceo') {
        // do code for ceo
    }
    else if (this.checked && this.id === 'bod') {
            //do code for bod;
    }
    else{
       //do code;
    }
});

2 Comments

Nitya. Suppose I have 100 checkboxes then how about your code?
Please edit with more information. Code-only and "try this" answers are discouraged, because they contain no searchable content, and don't explain why someone should "try this".

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.