...

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



Submerged
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 :)

Thanks,
Submerged

Kor
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]]
obj!=b?b.options[0].selected=true:null;
}
obj.form['choise'].value=obj.value
}
onload = function(){
for(var i=0;i<n.length;i++){
obj.form[n[i]].onchange=function(){changeV(this)}
}
}
</script>

Submerged
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 :)

Kor
05-30-2006, 02:49 PM
yes, if you want to, you may remove the
onload=function(){
...
}

and simply add to your all list boxes the event

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

Submerged
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 :)

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

Submerged
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 :)

Kor
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...

Submerged
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 :)

Submerged
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 :)

_bullseye_
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(){
//alert(a.value);
// 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;
}


alert(message);
return false;

}


function updateCategories(list,num) {
var f = window.document.form1;
f1=f.AutoSpecial;
f2=f.BigDealAd;
f3=f.YellowPages;
f4=f.Announcements;
f5=f.Instruction;
f6=f.Financial;
f7=f.PetsLivestock;
f8=f.Merchandise;
f9=f.Recreation;
f10=f.Automotive;
f11=f.RealEstateRentals;
f12=f.RealEstateSales;


f1.selectedIndex=0;
f2.selectedIndex=0;
f4.selectedIndex=0;
f5.selectedIndex=0;
f6.selectedIndex=0;
f7.selectedIndex=0;
f8.selectedIndex=0;
f9.selectedIndex=0;
f10.selectedIndex=0;
f11.selectedIndex=0;
f12.selectedIndex=0;
list.selectedIndex=num;
}

</script>
</CODE>

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

<CODE>

<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>
</select>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum