Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New to the CF scene
    Join Date
    Jan 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Changing a variable with a dropdown onChange function

    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

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    PHP Code:
    <script type="text/javascript">
    var 
    =['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> 
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    New to the CF scene
    Join Date
    Jan 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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)">
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    New to the CF scene
    Join Date
    Jan 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

    PHP Code:
    <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

  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    I don't understand why you try to complicate the job. Isn't that code good enough?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #7
    New to the CF scene
    Join Date
    Jan 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #8
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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...
    Last edited by Kor; 05-31-2006 at 04:03 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #9
    New to the CF scene
    Join Date
    Jan 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #10
    New to the CF scene
    Join Date
    Jan 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oh, I jsut found the old forum after a desperate Google attempt, here is the code they used:
    PHP Code:
    <select name='choice1' onchange="this.form.choice2.selectedIndex=0;this.form.choice3.selectedIndex=0;this.form.choice.value=this.value"
    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

  • #11
    New to the CF scene
    Join Date
    Sep 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Drop down selection onChange resets all other dropdowns

    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:

    Code:
    <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>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •