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 1 ids starting 1

also reset checked status when click initiated! use i instead of stars in getelementbyid

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

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -