...

View Full Version : Dropdown from array



mavro
09-24-2011, 03:24 PM
Hi, I want to set a different value for each name and use url for values.
Can somebody help me with this? :confused:

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


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

DaveyErwin
09-24-2011, 05:32 PM
This is not the solution for
your homework problem but
maybe it will help you see
the way to go ?


<!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>

DaveyErwin
09-24-2011, 07:19 PM
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.




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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum