php_novice2007 Posted August 28, 2007 Share Posted August 28, 2007 hi, I've got the following <input type=checkbox name="u0001" unchecked>u0001<br> <input type=checkbox name="u0002" unchecked>u0002<br> <input type=checkbox name="u0003" unchecked>u0003<br> <input type=checkbox name="u0004" unchecked>u0004<br> and I want to use javascript to go through all the check boxes and see which one is checked. The check box names are stored in an array called unitArray. unitArray[0] = "u0001", unitArray[1] = "u0002" etc. In my code I've got for (var i = 0; i < unitArray.length; i++) { var t = "myform." + unitArray[i] + ".checked"; if (t) { } } But this does not work. It says that myform.unitArray has no properties. Seems like the if statement just sees a string and say it is true because it is non empty. How do I do what I want..? Thanks~! Link to comment https://forums.phpfreaks.com/topic/67023-converting-variables/ Share on other sites More sharing options...
mainewoods Posted August 28, 2007 Share Posted August 28, 2007 var t, ua; for (var i = 0; i < unitArray.length; i++) { ua = unitArray[i]; //not sure if this intermediate step is necessary! t = document.myform[ua].checked; //uses dom [] array notation access if (t) { } } Link to comment https://forums.phpfreaks.com/topic/67023-converting-variables/#findComment-336358 Share on other sites More sharing options...
php_novice2007 Posted August 28, 2007 Author Share Posted August 28, 2007 Hi, Thanks for that, that part works now. Now I have a series of radio buttons [code] <input type=radio name="u0001choice" checked value="u0001 yes">yes <input type=radio name="u0001choice" value="u0001 no">no <input type=radio name="u0002choice" checked value="u0002 yes">yes <input type=radio name="u0002choice" value="u0002 no">no <input type=radio name="u0003choice" checked value="u0003 yes">yes <input type=radio name="u0003choice" value="u0003 no">no <input type=radio name="u0004choice" checked value="u0004 yes">yes <input type=radio name="u0004choice" value="u0004 no">no And I want to go through the list of unitArray again and see which button is selected.. I tried for (var i = 0; i < unitArray.length; i++) { var t2 = unitArray[i] + "choice"; var t3 = document.myform[t2][1].checked; var t4 = document.myform[t2][1].value; if (t3) { alert("For" + unitArray[i] + " " + t4 + " is chosen"); } } But nothing is being alerted } [/code] Link to comment https://forums.phpfreaks.com/topic/67023-converting-variables/#findComment-336623 Share on other sites More sharing options...
mainewoods Posted August 29, 2007 Share Posted August 29, 2007 -do not use 'var' inside a loop, use 'var' before the loop -your extra '[1]' was not valid var t2, t3, t4; for (var i = 0; i < unitArray.length; i++) { t2 = unitArray[i] + "choice"; t4 = document.myform[t2].value; // it should alert a value like 'u0001 yes' or 'u0001 no' but not both alert("For " + t2 + " " + t4 + " is chosen"); } Link to comment https://forums.phpfreaks.com/topic/67023-converting-variables/#findComment-337344 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.