Click to See Complete Forum and Search --> : Setting multiple option boxes to the same value
Hi,
What I want to to do is this: I have a number of drop down combo boxes and want to be able to set them all to the same value with one click, I want them all to have the same value as the first option box (a sort of synchronizing). So I tried some stuff, but with no luck... My drop down combo boxes are called n_link[0] to n_link[x], yes a real array because I use it with PHP.
Here's what I got so far, the form is called 'bplisting':
function GlobalChoice(choice)
{
var objForm = document.bplisting;
var intIndex1 = objForm.elements[choice].selectedIndex;
var theElems = objForm.n_link[0].length;
for (i=1;i<theElems.length;i++)
{
objForm.elements[n_link[i]].selectedIndex = intIndex1;
}
}
Could someone tell me what I am doing wrong? At the moment I am getting an error 'n_link.0 is null or not an object'. What is submitted to the function is the name of the first option box (with which the rest has to synchronize).
Big thanks in advance.
PS: All the drop down combo boxes have the same amount of options, with the same values, so they're all the same (it's for assigning attributes, so the list from which to choose from is the same everywhere.)
gil davis
04-08-2003, 09:03 AM
You have to place quotes around the php array names.objForm.elements["n_link[" + i + "]"].selectedIndex
khalidali63
04-08-2003, 09:07 AM
here is how I'd probably do( if I need to do such a thing..:D )
anouter loop for the listbox array and an internal loop for setting up the elements.
var listboxes = doocument.n_link;
for(outer=0;outer<listboxes.length;outer++){
for(inner=0;inner<listboxes[outer].length;inner++){
listboxes[outer].options[inner].text = "someval";
listboxes[outer].options[inner].value= "someval";
}
}
I have not tested thios,but prety sure it will work
Cheers
Khalid
Thanks for the input, but it didn't solve it... Still gives errors.
If I add quotes around the n_link's in the second and third line (the var intIndex1 and theElems) then I get new errors...
Khalid: Ehm, I know how to create them (PHP does a good easy job at that), it's the selection with which I have problems.
I now have this (should I add quotes around all the n_link's?):
function GlobalChoice()
{
var objForm = document.bplisting;
var intIndex1 = objForm.n_link[0].selectedIndex;
var theElems = objForm.n_link[0].length;
alert(theElems);
alert(intIndex1);
for (i=1;i<theElems.length;i++)
{
objForm.elements[\"n_link[\" + i + \"]\"].selectedIndex = intIndex1;
}
}
gil davis
04-08-2003, 09:51 AM
If you would post the actual HTML results of the PHP it would help.
Okay, sorry for the vagueness.
Here goes (in short):
<script>
function GlobalChoice()
{
var objForm = document.bplisting;
var intIndex1 = objForm.n_link[0].selectedIndex;
var theElems = objForm.n_link[0].length;
alert(theElems);
alert(intIndex1);
for (i=1;i<theElems.length;i++)
{
objForm.elements[\"n_link[\" + i + \"]\"].selectedIndex = intIndex1;
}
}
</script>
<input type='button' onClick=\"GlobalChoice()\" value='GC'>
<form method='Post' action='*FILE*' name='bplisting'>
<select name='n_link[0]' Class='adminscrolldown'>
<option value='' >--</option>
<option value='alpha'>alpha</option>
<option value='beta'>beta</option>
</select>
<select name='n_link[1]' Class='adminscrolldown'>
<option value='' >--</option>
<option value='alpha'>alpha</option>
<option value='beta'>beta</option>
</select>
<select name='n_link[3]' Class='adminscrolldown'>
<option value='' >--</option>
<option value='alpha'>alpha</option>
<option value='beta'>beta</option>
</select>
.
.
.
</form>
And then I want to be able to set the first (n_link[0]) to a value (for example alpha) and then with one click set /focus all the other's (n_link[n]) to that same value.
gil davis
04-08-2003, 11:27 AM
for (i=1;i<theElems.length;i++)
This confused me because theElems.length does not appear to have anything to do with the number of n_links that are in your example.
objForm.elements[\"n_link[\" + i + \"]\"].selectedIndex = intIndex1;
Why do you think you have to escape the quotes in this statement? You are building a string inside the brackets, and quotes are necessary in the logic of this context.
I had no problem marching through the array using this:for (var i=0; i<=2; i++) {
it = document.bplisting["n_link[" + i + "]"];
alert(it.options[it.selectedIndex].value);
}(I used the variable "it" as a shortcut)
Because of PHP... I copied it from the PHP source. :(
In the output it's without the escapes.
Anyways: I need the length too since I don't know how many drop down combo boxes there are... That varies from page to page. I realize that I counted the wrong part of the form though.
I'll see whether I can make it work for a fixed number and then try to let it loop through all of them then.
YEEEEEEEEEEEEEEEESSSSSSSSSSSSSSSSSS! Big thanks gil!
After some tweaking... It works now, well for a set length. :)
Here's the final code (function alone):
function GlobalChoice()
{
one = document.bplisting["n_link[" + 0 + "]"];
oneindex = one.selectedIndex;
for (i=1; i < 50; i++) {
it = document.bplisting["n_link[" + i + "]"];
it.selectedIndex = oneindex;
}
}
gil davis
04-08-2003, 12:28 PM
Perhaps this will help:
var done = false;
var i = 1;
while (!done) {
it = document.bplisting["n_link[" + i + "]"];
if (it)
{it.selectedIndex = oneindex;
i++;}
else
{done = true;}
}
Indeed! :) We have a winner.
Thanks very much for the input and thoughts. Perfect!