seventheyejosh Posted May 19, 2009 Share Posted May 19, 2009 hey guys, im trying to find the value of a radio button group, with id / name of 'chart'. all the examples i can find online are like: var val = 0; for( i = 0; i < document.myform.mygroup.length; i++ ) { if( document.myform.mygroup.checked == true ) val = document.myform.mygroup.value; } alert( "val = " + val ); which is fine, except i'd rather not base it of of the name of the radio, and the items arent in a form. im learning js still so im not perfect at it but here is kinda what i need: for (var i=0; i<document.getElementById('chart').length; i++) { if (document.getElementById('chart').checked) { var chart = document.getElementById('chart').value; break; } } alert('chart - '+chart); obv im having issues with the ('chart') part but i tried ('chart').value and ('chart')..value... but to no avail this is the radio setup <td align=center class=headercell><input type=radio name=chart id=chart value=steps {$step_c} onmouseup="Updatechart_two('steps')">Step Comparison</td> <td align=center class=headercell><input type=radio name=chart id=chart value=thismonth {$thismonth_c} onmouseup="Updatechart_two('thismonth')">Current Month</td> <td align=center class=headercell><input type=radio name=chart id=chart value=lastmonth {$lastmonth_c} onmouseup="Updatechart_two('lastmonth')">Last Month</td> <td align=center class=headercell><input type=radio name=chart id=chart value=alltime {$alltime_c} onmouseup="Updatechart_two('alltime')">All-time Monthly</td> the reason for {$var} is smarty checked/unchecked.. but that is a non issue. thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/158785-solved-radio-button-value-no-form/ Share on other sites More sharing options...
KevinM1 Posted May 19, 2009 Share Posted May 19, 2009 You can't use getElementById() in this case because an id is a unique identifier. So, JavaScript expects that you're referencing only one element when you use an id. You'll need to grab a hold of the radio buttons by name. It's not too hard to do: var radioArray = new Array(); var inputs = document.getElementsByTagName('input'); var chartOutput = ''; for(var i = 0; i < inputs.length; i++) { if(inputs[i].name == 'chart') { radioArray.push(inputs[i]); } } /* now you can check to see which button has been pressed */ for(var j = 0; j < radioArray.length; j++) { if(radioArray[j].checked) { chartOutput += " " + radioArray[j].value; } } alert("Chart -" + chartOutput); So, you'll need to run through all of the inputs on the page and store those that represent the radio buttons. Then, you'll need to go through that grouping and obtain the value of the checked button. Quote Link to comment https://forums.phpfreaks.com/topic/158785-solved-radio-button-value-no-form/#findComment-837479 Share on other sites More sharing options...
seventheyejosh Posted May 19, 2009 Author Share Posted May 19, 2009 fantastic! thanks very much, just what i needed! Quote Link to comment https://forums.phpfreaks.com/topic/158785-solved-radio-button-value-no-form/#findComment-837490 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.