0

How can I set html radio value from javascript ?? I try this code

<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">

$.getJSON("getquestion.php",function(result)
{
        //set radio value
    //$("#choice").html(result.test_choice_a);
    //$("#choice").val(result.test_choice_a);
    //$("#choice").html(result.test_choice_b);
    //$("#choice").val(result.test_choice_b);
    //$("#choice").html(result.test_choice_c);
    //$("#choice").val(result.test_choice_c);
    //$("#choice").html(result.test_choice_d);
    //$("#choice").val(result.test_choice_d);
    document.testform.choice[0].value = result.test_choice_a;
    document.testform.choice[1].value = result.test_choice_b;   
    document.testform.choice[2].value = result.test_choice_c;   
    document.testform.choice[3].value = result.test_choice_d;
}
</script>

<input type="radio" name="choice" id="choice" ><br>
<input type="radio" name="choice" id="choice"><br>
<input type="radio" name="choice" id="choice"><br>
<input type="radio" name="choice" id="choice"><br>

but it didn't work.

How can I set it ?? help me please.

1
  • 3
    There's a missing ); at the end of your function call. Commented Jul 30, 2012 at 2:42

3 Answers 3

1

Maybe the content hasn't loaded yet put your ajax call in a $(document).ready()

$(document).ready(function(){
    $.getJSON("getquestion.php",function(result)
    {
            //set radio value
        //$("#choice").html(result.test_choice_a);
        //$("#choice").val(result.test_choice_a);
        //$("#choice").html(result.test_choice_b);
        //$("#choice").val(result.test_choice_b);
        //$("#choice").html(result.test_choice_c);
        //$("#choice").val(result.test_choice_c);
        //$("#choice").html(result.test_choice_d);
        //$("#choice").val(result.test_choice_d);
        document.testform.choice[0].value = result.test_choice_a;
        document.testform.choice[1].value = result.test_choice_b;   
        document.testform.choice[2].value = result.test_choice_c;   
        document.testform.choice[3].value = result.test_choice_d;
    });
});

Also element ids should be unique.

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

Comments

0

You need to give every radio a uniquie id.

<script type="text/javascript">
$.getJSON("getquestion.php",function(result) {
    //set radio value
    $("#choice_1").val(result.test_choice_a);
    $("#choice_2").val(result.test_choice_b);
    $("#choice_3").val(result.test_choice_c);
    $("#choice_4").val(result.test_choice_d);
}
</script>

<input type="radio" name="choice" id="choice_1" ><br>
<input type="radio" name="choice" id="choice_2"><br>
<input type="radio" name="choice" id="choice_3"><br>
<input type="radio" name="choice" id="choice_4"><br>

Comments

0

You can't give an id to many elements

    $(document).ready(function () {
        $.getJSON("getquestion.php", function (result) {
            $("#choice_1").val(result.test_choice_a);
            $("#choice_2").val(result.test_choice_b);
            $("#choice_3").val(result.test_choice_c);
            $("#choice_4").val(result.test_choice_d);
        });
    });

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.