/    Sign up×
Community /Pin to ProfileBookmark

Select function

Hi,

I have a very simple problem which I hope someone can help me with and excuse my ignorance of such things.

If I create a form with a select option how do I then create a second select with options determined by the user’s first choice?

For argument’s sake, if my first list includes number of counties and Gloucestershire was selected the second option would list towns within that region, including Gloucester, Cheltenham, etc.

Thanks in antipation.

Smudgey

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@KorJul 25.2006 — Something like this?
[code=php]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
var arr =[];//all the data's unsorted array
arr['CANADA']=['Alberta','British Columbia','Manitoba','New Brunswick'];
arr['USA']=['Alabama','Alaska','Arizona','Arkansas','California','Colorado'];
//you may add as many other elements as you want

function create(s){
var root= document.getElementById('states');
while(root.hasChildNodes()){//removes the old options
root.removeChild(root.childNodes[0])
}
if(s.selectedIndex>0){//if a country is selected
var v=s.value;
for(var i=0;i<arr[v].length;i++){
var oOpt = document.createElement('option');//creates element option
oOpt.setAttribute('value',arr[v][i]);//sets the value
oOpt.appendChild(document.createTextNode(arr[v][i]));//sets the text
root.appendChild(oOpt);//appends the option to the select element
}
}
}
</script>
</head>
<body>
<select id="countries" onchange="create(this)">
<option>-- select a country --</option>
<option value="CANADA">CANADA</option>
<option value="USA">USA</option>
</select>

<select id="states">
</select>
</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@SmudgeyauthorJul 25.2006 — That's it, brilliant - many thanks Kor. Problem solved.

? Smudgey
×

Success!

Help @Smudgey spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...