Salah a a a Al Jasem
asked on
looping through a radio groop to use the selected radio serial in getting the listbox item
I am looping within a radio group, getting the selected radio and then using it's order to get the data from a listbox, so if the selected radio is number 5 then textbox1 must contain item 5 of the listbox1, here is my code (which is not working)
<script>
function myFunction() {
var AllListItems = document.getElementById("L istBox1");
var WantedRadio = document.getElementsByName ("CheckedR adio");
var RadioGroupLength = WantedRadio .length;
for (var i = 0; i < RadioGroupLength ; i++) {
if (WantedRadio[i].type == 'radio' && WantedRadio[i].checked == true)
document.getElementById("T extBox1"). value = inputfields[i].value; //this line works fine
document.getElementById("T extBox1"). value = AllListItems.options[i].va lue; //this line does not work
}
}
</script>
<script>
function myFunction() {
var AllListItems = document.getElementById("L
var WantedRadio = document.getElementsByName
var RadioGroupLength = WantedRadio .length;
for (var i = 0; i < RadioGroupLength ; i++) {
if (WantedRadio[i].type == 'radio' && WantedRadio[i].checked == true)
document.getElementById("T
document.getElementById("T
}
}
</script>
Did you miss the curly braces ?
So replace :
So replace :
if (WantedRadio[i].type == 'radio' && WantedRadio[i].checked == true)
document.getElementById("TextBox1").value = inputfields[i].value; //this line works fine
document.getElementById("TextBox1").value = AllListItems.options[i].value; //this line does not work
by : if (WantedRadio[i].type == 'radio' && WantedRadio[i].checked == true) {
document.getElementById("TextBox1").value = inputfields[i].value; //this line works fine
document.getElementById("TextBox1").value = AllListItems.options[i].value; //this line does not work
}
Here is why your function was not working
You have an if statement (checking for a checked state on the radio button) but you don't enclose the following statements in { }, So the first line fires only when a checked radio is found - the second executes on each iteration of the loop.
This code should work
General rule - always use { } for your code blocks on an if - even if there is only one line - it will save you from errors like these.
You have an if statement (checking for a checked state on the radio button) but you don't enclose the following statements in { }, So the first line fires only when a checked radio is found - the second executes on each iteration of the loop.
This code should work
function myFunction() {
var AllListItems = document.getElementById("ListBox1");
var WantedRadio = document.getElementsByName("CheckedRadio");
for (var i = 0; i < WantedRadio.length ; i++) {
if (WantedRadio[i].checked) {
document.getElementById("TextBox1").value = inputfields[i].value; //this line works fine
document.getElementById("TextBox1").value = AllListItems.options[i].value; //this line does not work
}
}
}
Bear in mind this will put the option VALUE in the text box - not the displayed text.General rule - always use { } for your code blocks on an if - even if there is only one line - it will save you from errors like these.
why keep looping once it has been found?
for (var i = 0; i < RadioGroupLength ; i++) {
if (WantedRadio[i].type == 'radio' && WantedRadio[i].checked == true)
{
document.getElementById("TextBox1").value = inputfields[i].value; //this line works fine
document.getElementById("TextBox1").value = AllListItems.options[i].value;
break;
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I should have used style="visibility:hidden" to make listbox not appear and to have access to the listbox contents
On reflection the function code makes no sense with or without { }
Either way
This
Either way
This
document.getElementById("TextBox1").value = inputfields[i].value; //this line works fine
Will always get overwritten by thisdocument.getElementById("TextBox1").value = AllListItems.options[i].value; //this line does not work
HTML
Open in new window
jQueryOpen in new window
EDIT: Fix code error in JQueryYou can see this working here
I will post a JavaScript only version shortly