ucas
ucas

Reputation: 487

Selecting radio button

Here is jquery script used to select radio button.

function radio(){
       var presetValue = "scholar";
$("[name=identity]").filter("[value="+presetValue+"]").attr("checked","checked");
}

The scrip is invoked on page load. This is XHTML peace of code:

<label for="scholar"> Scholar </label>
        <input type="radio" name="identity" id="scholar" value="scholar"  />
        </td>

        <td>
        <label for="oracle"> Oracle </label>
        <input type="radio" name="identity" id="oracle" value="oracle"/>
        </td>

        <td>
        <label for="both"> Both </label>
        <input type="radio" name="identity" id="both" value="both"/>

When the program runs that 'scholar' radio button is not selected. What might be the problem? Best regards

Upvotes: 0

Views: 528

Answers (3)

Bertrand Marron
Bertrand Marron

Reputation: 22210

If you're using jQuery >= 1.6, you should use prop().

Also, you're using a complicated selector for nothing. Since your inputs have id's, this will do it:

$('#scholar').prop('checked', true);

If presetValue changes (i.e: you simplified the code), do this:

$('#' + presetValue).prop('checked', true);

Upvotes: 1

Liam Bailey
Liam Bailey

Reputation: 5905

function radio(){
       var presetValue = "scholar";
$("input[value="+presetValue+"]").attr("checked",true);
}

Upvotes: 0

Dan Smith
Dan Smith

Reputation: 5685

Check out this answer: how to set radio option checked onload with jQuery

Upvotes: 0

Related Questions