javascript - Dynamically, check radio button with for loop -
i'm trying create 5 star radio picker.
this 1 of radio buttons:
<input onclick="check(3)" type="radio" name="three" id="num_3_star" value="3">
after method gets called , "3" passed on javascript function:
function check(stars) { for(i = 0 ; < stars; i++) { document.getelementbyid("num_" + stars + "_star").checked = true; } }
when run code, not perform checked = true, though if remove loop, works fine.
any ideas why loop preventing checking radio boxes?
this entire code:
<script> function check(stars) { for(i = 0 ; < stars; i++) { document.getelementbyid("num_" + stars + "_star").checked = true; } } </script> <form> <input onclick="check(1)" type="radio" name="one" id="num_1_star" value="1"> <input onclick="check(2)" type="radio" name="two" id="num_2_star" value="2"> <input onclick="check(3)" type="radio" name="three" id="num_3_star" value="3"> <input onclick="check(4)" type="radio" name="four" id="num_4_star" value="4"> <input onclick="check(5)" type="radio" name="five" id="num_5_star" value="5"> </form>
thanks!
i
had 1ids
starting 1also reset
checked
status when click initiated! usei
instead ofstars
ingetelementbyid
try this:
function check(stars) { (var j = 1; j <= 5; j++) { document.getelementbyid("num_" + j + "_star").checked = false; } (var = 1; <= stars; i++) { document.getelementbyid("num_" + + "_star").checked = true; } }
<input onclick="check(1)" type="radio" name="one" id="num_1_star" value="1"> <input onclick="check(2)" type="radio" name="two" id="num_2_star" value="2"> <input onclick="check(3)" type="radio" name="three" id="num_3_star" value="3"> <input onclick="check(4)" type="radio" name="four" id="num_4_star" value="4"> <input onclick="check(5)" type="radio" name="five" id="num_5_star" value="5">
Comments
Post a Comment