1

Why doesn't this work properly:

<input name="Fuel" type="radio" value="" title="Any" checked="checked">
<input name="Fuel" type="radio" value="1" title="Petrol">
<input name="Fuel" type="radio" value="2" title="Diesel">

<div class="options">Any</div>
<div class="options">Petrol</div>
<div class="options">Diesel</div>

$('.options').click(function(){
    $('input').removeAttr('checked');
    $('input[title="' + $(this).text() + '"]').attr('checked', 'checked');   
});

See the working example here:

http://jsfiddle.net/4MTL2/

It works ok until you try to re-select a button that has already been selected.

2 Answers 2

1

Try .prop( propertyName, value )

value Type: String or Number or Boolean A value to set for the property.

$('.options').click(function () {
    $('input[title="' + $(this).text() + '"]').prop('checked', true);
});

Fiddle Demo

Sign up to request clarification or add additional context in comments.

3 Comments

Godlike. Thanks. I'll accept the answer as soon as Stackoverflow allows. Thanks for you help.
Read Attributes vs. Properties will help you understand more.
Nice one. Thanks for that link too. That starts to make a lot more sense now.
0

Best solution is use .click() Because it trigger click radio event as well, which is very important

The other solution that only change radio option property or attribute

will NOT trigger radio event !!!!!!!!

  $("#your_radio_option_ID_here").click()

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.