View Full Version : Changing a variable with a dropdown onChange function

05-30-2006, 12:45 AM
Okay, this is a bit of a complicated situation, so hang with me please :)

On my script, I have 4 drop down boxes, called "choice1","choice2","choice3","choice4". Then I also have a hidden value called "choice".

What I need is that whenver someone picks a value out of any of the 4 dropdowns, it does 2 things:

1. Sets all 3 of the other dropdown boxes back to their top (original) value.
2. Sets the value of choice (hidden) to that of the selected option.

This is a bit beyond my knowledge, I don't use javascript too much. Any help is VERY appreciated :)


05-30-2006, 09:30 AM
<script type="text/javascript">
var n =['choise1','choise2','choise3','choise4'];
function changeV(obj){
for(var i=0;i<n.length;i++){
var b=obj.form[n[i]]
onload = function(){
for(var i=0;i<n.length;i++){

05-30-2006, 02:28 PM
hmmmm... lol, that looks like greek to me. If im not mistaken, i think there should be a way to just add an onChange event to the select part, like:

<select name=choice1 onChange=(change other 3 boxes and Choice here)>

I saw code like this a while ago, I just dont quite remember the syntax involved :)

05-30-2006, 02:49 PM
yes, if you want to, you may remove the

and simply add to your all list boxes the event

<select ..... onchange="changeV(this)">

05-30-2006, 11:20 PM
Haha... well, I don't understand how it works, but it seems too.

Is there a way to have the code entirely in the onChange part of the select function? I saw code to do this a few months ago, it would work better for my purposes, I just dont remember the syntax. For instance, the part that would change the hidden value (choice) to the object selected would be something like this:

<select name=\"choice$labelselect\" onChange=\"document.form.choice.value=obj.value\">
If I could just get code to change, say, choice2 back to the top/initial value, and the hidden value "choice" to whatever object was selected, i can take it from there. Thanks again for all of your help :)

05-31-2006, 09:51 AM
I don't understand why you try to complicate the job. Isn't that code good enough?

05-31-2006, 01:25 PM
well, it did work, but i dont really understand how it worked, the old code i understood welle nough to change easily. Also, there will actually be 4 groups of 4 dropdown boxes on this page, so it would be easier to set them all up. besides, the old code i aw was way less complicated then what i see here. i mean, ill definetly use it and be grateful for the help, but i just wanted to know if anyone knew the other method :)

05-31-2006, 03:58 PM
What you don't understand? I will explain you detailed. I guess that you should not restrict yourself to the things you saw sometime ago....... programing, and javascript especially, do evolve in time...

The way you want would mean writting exactly the same code, but written locally 4 times, for each of your 4 select objects. Does this seems logical to you? Further more, a function written as embeded will do the job nomatter how many objects you have, just insert the names in the array...

06-01-2006, 04:43 AM
I saw the old code about a month ago, it should still work... anyways, ive looked this over, and i think I should be able to decode it for my usage. Thanks for the help :)

06-01-2006, 04:53 AM
Oh, I jsut found the old forum after a desperate Google attempt, here is the code they used:

<select name='choice1' onchange="this.form.choice2.selectedIndex=0;this.form.choice3.selectedIndex=0;this.form.choice.value=this.valu e">This would be the select function for one dropdown box, it would be repeated for each box switching the other form numbers. I use a PHP for loop with variables, so it is only one line of code anyways. Just letting you know what I did see in case you found it interesting :)

09-12-2006, 04:54 PM
I was using this code successfully before adding 2 more dropdowns, and now it is broke. I can't figure out why. Any clues?

I have this in my head:

<script Language="JavaScript" type="text/javascript">

// Create global variables for dropdown list
var f1="";
var f2="";
var f3="";
var f4="";
var f5="";
var f6="";
var f7="";
var f8="";
var f9="";
var f10="";
var f11="";
var f12="";

function chooseCategory(){
// a.value = the option selected, ex. None-0
message = "Please choose a category from our drop down lists";
if(f1!="") {
return true;
if(f2!="") {
return true;
if(f3!="") {
return true;
if(f4!="") {
return true;
if(f5!="") {
return true;
if(f6!="") {
return true;
if(f7!="") {
return true;
if(f8!="") {
return true;
if(f9!="") {
return true;
if(f10!="") {
return true;
if(f11!="") {
return true;
if(f12!="") {
return true;

return false;


function updateCategories(list,num) {
var f = window.document.form1;



And here is an example of one of my 12 dropdowns. What am i missing here?


<select name="AutoSpecial" size=1 id="AutoSpecial" onChange="updateCategories(this,this.selectedIndex);">
<option selected value="None - 0">None - 0 </option>
<option value="Antique Autos - 91"> Antique Autos - 91 </option>
<option value="Domestic Autos - 92"> Domestic Autos - 92 </option>
<option value="SUVs & 4x4s - 96">SUVs &amp; 4x4s - 96 </option>
<option value="Foreign Autos - 93"> Foreign Autos - 93 </option>
<option value="Motorcycles/Scooters - 94"> Motorcycles/Scooters - 94 </option>
<option value="Trucks + Vans - 95">Trucks &amp; Vans - 95 </option>

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum