/    Sign up×
Community /Pin to ProfileBookmark

Can I populate a dropdownl list???

I have 2 dropdown list in my html. Is possible for me to populate the option values in the second dropdown list depend on what I select in the first one? Thank you.

zorro

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@HaganeNoKokoroSep 13.2004 — Sure it's possible
<i>
</i>var listValues = ["Option1,1", "Option2,2", "Option3,3"]
function populateList(id) {
var pList = document.getElementById(id);

//delete old options
for(var i=0; i&lt;pList.options.length; i++) pList.remove(0);

//create new option (repeat the stuff below as neccesary)
for(var i=0; i&lt;listValues.length; i++) {
var obj = document.createElement("OPTION");
var valtext=listValues[i].split(",");
//fill in option data
obj.text=valtext[0];
obj.value=valtext[1];
//add option to select
pList.add(obj);
}
}

Of course, you will want to modify this a little so it has multiple option/value sets for each of the options on the first menu, but this will give you a good idea of how to start. Also keep in mind that if you want your form to be usable for people without javascript, you will have to have all options in the second list and then remove them in the page onload handler, so that they will be available for non-JS users.
Copy linkTweet thisAlerts:
@Warren86Sep 13.2004 — zorrox02:

Here's an example of how to add or remove items from a select list.

<HTML>

<Head>

<Script Language=JavaScript>

function insertOption(isList,isValue,isText){

isData = new Option(isText,isValue);
isList.add(isData,isList.options.length);
}

function removeOption(isList,removeItem){

isList.selectedIndex = removeItem;
isList.remove(isList.selectedIndex);
isList.selectedIndex = 0;
}

function getSelection(isList){

isValue = isList.options[isList.selectedIndex].value;
alert(isValue)
}


</Script>

</Head>

<Body>

<br><br>

<center>

<select name='List1' onChange="getSelection(this)">

<option value='null' selected> Make a selection </option>

<option value='1st'> First </option>

<option value='2nd'> Second </option>

</select>

<input type=button value='Insert New Option' onClick="insertOption(List1,'3rd','Third')">

<input type=button value='Remove Last Option' onClick="removeOption(List1,3)">

</center>

</Body>

</HTML>
×

Success!

Help @zorrox02 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.17,
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,
)...