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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Location
    Thessaloniki Greece
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Dropdown from array

    Hi, I want to set a different value for each name and use url for values.
    Can somebody help me with this?

    eg: name A3 and value http://www.audi.com

    Code:
    <script type="text/javascript">
    
    function dynamic1(parent,child){
    
    	var parent_array = new Array();
    
    	parent_array[''] = ['Please select a manufacturer'];
    
    	parent_array['Audi'] = ['A3','A4','A5','A6','A8','Q5','Q7','S3','S4','S5','S6','S8','RS6'];
    
    	parent_array['Dacia'] = ['Sandero','Logan'];
    
    	parent_array['FIAT'] = ['Bravo','Punto','Grande Punto'];
    
    	parent_array['Peugeot'] = ['207','308','407','607'];
    
    	parent_array['SEAT'] = ['Ibiza','New Ibiza','Leon'];
    
    	parent_array['Skoda'] = ['Fabia','Octavia Tour','Octavia 2','Superb'];
    
    	var thechild = document.getElementById(child);
    
    	thechild.options.length = 0;
    
    	var parent_value = parent.options[parent.selectedIndex].value;
    
    	if (!parent_array[parent_value]) parent_value = '';
    
    	thechild.options.length = parent_array[parent_value].length;
    
    	for(var i=0;i<parent_array[parent_value].length;i++){
    
    		thechild.options[i].text = parent_array[parent_value][i];
    
    		thechild.options[i].value = parent_array[parent_value][i];} }
    
    </script>

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    970
    Thanks
    19
    Thanked 212 Times in 210 Posts
    This is not the solution for
    your homework problem but
    maybe it will help you see
    the way to go ?

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta name="generator" content="daveyerwin">
    <title>Untitled</title>
    
    <script type="text/javascript">
    var parent;
    var child;
    var parentObject = {};
    	parentObject['Please select a manufacturer'] = [];
    	parentObject['Audi'] = ['A3','A4','A5','A6','A8','Q5','Q7','S3','S4','S5','S6','S8','RS6'];
    	parentObject['Dacia'] = ['Sandero','Logan'];
    	parentObject['FIAT'] = ['Bravo','Punto','Grande Punto'];
    	parentObject['Peugeot'] = ['207','308','407','607'];
    	parentObject['SEAT'] = ['Ibiza','New Ibiza','Leon'];
    	parentObject['Skoda'] = ['Fabia','Octavia Tour','Octavia 2','Superb'];
    function init(){
     	parent = document.getElementById("Make");
        	child = document.getElementById("Model")       
    	for(var i in parentObject){
    		parent.options[parent.options.length]=new Option(i,i);
    	}
    }
    
    
    function dynamic1(make){
    	child.options.length = 0;
            if(make == 'Please select a manufacturer')return;	
    	for(var i = 0; i< parentObject[make].length; i++){
    		child.options[child.options.length] = new Option(parentObject[make][i]);
    	}
    }
    
    </script>
    
    
    </head>
    <body onload="init()">
    <div id="container" >
    <select id="Make" onchange="dynamic1(value)"></select>
    <select id="Model"></select>
    
    </div>
    </body></html>

  • #3
    Regular Coder
    Join Date
    Aug 2010
    Posts
    970
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Maybe this isn't home work ?
    Of course this doesn't work in real
    life because the urls are not
    true to life , except it does
    work for audi only.

    Code:
    <meta name="generator" content="daveyerwin">
    <title>Untitled</title>
    
    <script type="text/javascript">
    var parent;
    var child;
    var Maker;
    var Make;
    
    var autoMaker = {};
    autoMaker['Please select a manufacturer'] = [];
    autoMaker['Audi']={};
    autoMaker['Audi'].models=['A3','A4-sedan','A5-coupe','A6-sedan','a7','A8','Q5','Q7','r8'];
    autoMaker['Audi'].href='http://models.audiusa.com/'	;
    autoMaker['Dacia']={};
    autoMaker['Dacia'].models = ['Sandero','Logan'];
    autoMaker['Dacia'].href='http://www.dacia.com/';	
    
    function init(){
     	parent = document.getElementById("Make");
        	child = document.getElementById("Model")  
    	Maker = document.anchors[0];     
    	for(var i in autoMaker){
    		parent.options[parent.options.length]=new Option(i,autoMaker[i].href);
    	}
    }
    function dynamic1(href,make){
    	Make = make;
    	Maker.innerHTML = "";
    	child.options.length = 0;
    	child.options[child.options.length] = new Option('please select model')
            if(make == 'Please select a manufacturer')return;
    	var models = autoMaker[make].models;
    	var href = autoMaker[make].href;
    	for(var i = 0;i < models.length;i++){
    		child.options[child.options.length] = new Option(models[i],href+models[i])
    	}
    }
    function dynamic2(href,model){
    	Maker.innerHTML = "";
            if(model == 'please select model')return;
    	Maker.href = href;
    	Maker.innerHTML = Make + " " +model;	
    }
    </script>
    
    
    </head>
    <body onload="init()">
    <div id="container" >
    <select id="Make" onchange="dynamic1(value,options[selectedIndex].text)"></select>
    <select id="Model" onchange="dynamic2(value,options[selectedIndex].text)"></select>
    <a name="maker" href="#"></a>
    </div>
    </body></html>
    Last edited by DaveyErwin; 09-25-2011 at 03:07 AM.


  •  

    Posting Permissions

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