Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Radio Buttons "Checked" Attribute Not Working

The radio button does not show up as checked by default. I started off without a default choice doing some very simple js validation and it wasn't working. So I opted to just use default values until I figured that out and discovered that something weird is going on.

The markup is valid and I've tried in FF, Safari and Chrome. Nothing works.

I think it's a conflict with the jQuery library because the problem goes away when I remove the call script.

<label>Do you want to accept American Express?</label> Yes  <input id="amex" style="width: 20px;" type='radio' name='Contact0_AmericanExpress' value='1' /> No  <input style="width: 20px;" type='radio' name='Contact0_AmericanExpress' class='check' value='0' checked="checked" />
like image 889
Jordan Avatar asked Aug 31 '10 06:08

Jordan


People also ask

Does checked work for radio buttons?

If a radio button is checked, its checked property is true . Then, we assign the value of the selected radio button to the selectedSize variable. Since only one radio button in a radio group can be checked at a time, the loop is terminated immediately by the break statement.

Why radio button is not getting selected?

You cannot unselect radio buttons. That's because they're used if you want the user to select either option1 or option2 or option3 but prohibit selecting multiple values or leaving it empty (e.g. in case of selecting a Gender).


2 Answers

If you have multiple of the same name with the checked attribute it will take the last checked radio on the page.

<form>      <label>Do you want to accept American Express?</label>      Yes<input id="amex" style="width: 20px;" type="radio" name="Contact0_AmericanExpress"  />        maybe<input id="amex" style="width: 20px;" type="radio" name="Contact0_AmericanExpress"  checked="checked" />        No<input style="width: 20px;" type="radio" name="Contact0_AmericanExpress" class="check" checked="checked" />  </form>
like image 196
jeeves Avatar answered Sep 21 '22 05:09

jeeves


Radio inputs must be inside of a form for 'checked' to work.

like image 35
Michael Seltenreich Avatar answered Sep 21 '22 05:09

Michael Seltenreich