I have 3 of the 4 following script functions working. (See below)
The ‘getSBox(ids)’, getRBtn(nameinfo)’ and ‘getCBox(ids)’ all work
to return the value of the option selected within the element.
For consistency I want to create a ‘getRBtn(ids)’.
My attempt, commented out in the script, is called ‘getRBtnID(ids)’.
I am trying to determine the options array values from the id.
Does anyone have a suggestion(s) how I might do this in the test script below?
Thanks for any ideas. ?
[code=php]
<html>
<head>
<title>Selections Test</title>
<script type=”text/javascript”>
function getSBox(ids) {
var sel = document.getElementById(ids); // alert(sel+’nvalue:’+str);
var fnd = -1;
for (var i=0; i<sel.options.length; i++) { if (sel.selectedIndex == i) { fnd = i; } }
// return fnd; // return selected index of selection
// comment out next 3 lines if selectIndex used in line above
var str = “”;
if (fnd != -1) { str = sel.options[fnd].value; }
return str;
}
function getRBtn(nameinfo) {
var sel = document.getElementsByName(nameinfo); // alert(nameinfo+’n’+sel);
var fnd = -1;
var str = ”;
for (var i=0; i<sel.length; i++) {
if (sel[i].checked == true) { str = sel[i].value; fnd = i; }
}
// return fnd; // return option index of selection
// comment out next line if option index used in line above
return str;
}
/*
// This is the function that does not work correctly yet
function getRBtnID(ids) { // TEST FUNCTION FOR CONSISTENCY
var selobj = document.getElementByID(ids); // alert(ids+’n’+selobj);
// next line is questionable
var sel = selobj.document.getElementsByName(?????);
var fnd = -1;
var str = ”;
for (var i=0; i<sel.length; i++) {
if (sel[i].checked == true) { str = sel[i].value; fnd = i; }
}
// return fnd; // return option index of selection
// comment out next line if option index used in line above
return str;
}
*/
function getCBox(ids) {
var sel = document.getElementById(ids);
var fnd = false;
var str = ”;
if (sel.checked == true) { str = sel.value; fnd = true; } // alert(sel+’nvalue:’+str);
// return fnd; // return boolean status of checkbox
// comment out next line if checkbox boolean status used in line above
return str;
}
</script>
</head>
<body>
<h1>Selection Tests</h1>
<select id=”SBox”>
<option value=”>blank</option>
<option value=”Zero”>0</option>
<option value=”One”>1</option>
<option value=”Two”>2</option>
<option value=”Three”>3</option>
<option value=”Four”>4</option>
</select>
<button onClick=”document.getElementById(‘SBoxPick’).value=getSBox(‘SBox’);”>Report</button>
Result <input id=”SBoxPick” value=”” />
<p />
<input type=”radio” name=”RBtn” id=”RBox” value=”Five” />5
<input type=”radio” name=”RBtn” id=”RBox” value=”Six” />6
<input type=”radio” name=”RBtn” id=”RBox” value=”Seven” />7
<button onClick=”document.getElementById(‘RBoxPick’).value=getRBtn(‘RBtn’);”>Report</button>
Result <input id=”RBoxPick” value=” />
<p />
<input id=”CBox1″ type=”checkbox” value=”Eight” />8
<button onClick=”document.getElementById(‘CBox1Pick’).value=getCBox(‘CBox1′);”>Report</button>
Result <input id=”CBox1Pick” value=’false’ />
<br />
<input id=”CBox2″ type=”checkbox” value=”Nine” checked />9
<button onClick=”document.getElementById(‘CBox2Pick’).value=getCBox(‘CBox2′);”>Report</button>
Result <input id=”CBox2Pick” value=’true’ />
<br />
<input id=”CBox3″ type=”checkbox” value=”Ten” />10
<button onClick=”document.getElementById(‘CBox3Pick’).value=getCBox(‘CBox3′);”>Report</button>
Result <input id=”CBox3Pick” value=’false’ />
<p />
<script type=”text/javascript”>
var Msg = new Array();
Msg[0] = ”;
Msg[1] = “Element 1”;
Msg[2] = “Element 2”;
Msg[3] = “Element 3”;
Msg[4] = “Element 4”;
Msg[5] = “Element 5″;
document.write(‘<input type=”radio” id=”RadioButtons” name=”RButtons” value=”0″‘);
document.write(‘ onClick=”document.getElementById(‘RadioBtnPick’).value=Msg[getRBtn(‘RButtons’)]” />blank’);
document.write(‘<input type=”radio” id=”RadioButtons” name=”RButtons” value=”1″‘);
document.write(‘ onClick=”document.getElementById(‘RadioBtnPick’).value=Msg[getRBtn(‘RButtons’)]” />One’);
document.write(‘<input type=”radio” id=”RadioButtons” name=”RButtons” value=”2″‘);
document.write(‘ onClick=”document.getElementById(‘RadioBtnPick’).value=Msg[getRBtn(‘RButtons’)]” />Two’);
document.write(‘<input type=”radio” id=”RadioButtons” name=”RButtons” value=”3″‘);
document.write(‘ onClick=”document.getElementById(‘RadioBtnPick’).value=Msg[getRBtn(‘RButtons’)]” />Three’);
document.write(‘<input type=”radio” id=”RadioButtons” name=”RButtons” value=”4″‘);
document.write(‘ onClick=”document.getElementById(‘RadioBtnPick’).value=Msg[getRBtn(‘RButtons’)]” />Four’);
document.write(‘<input type=”radio” id=”RadioButtons” name=”RButtons” value=”5″‘);
document.write(‘ onClick=”document.getElementById(‘RadioBtnPick’).value=Msg[getRBtn(‘RButtons’)]” />Five’);
</script>
Report Result <input id=”RadioBtnPick” value=” size=”20″ />
</body>
</html>