Reputation: 716
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.
Upvotes: 0
Views: 589
Reputation: 1332
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);
});
});
Upvotes: 0
Reputation: 160833
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>
Upvotes: 0
Reputation: 97672
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.
Upvotes: 1