/    Sign up×
Community /Pin to ProfileBookmark

DYnamic select boxes?

Hello,

I have a select in which contains options A,B,C

I have another select box which contains optiosn 1,2,3,4,5,6

what I would like is when option B is selected in the first select box, then the secon select box should only display option 2.

if someone selects option A in the first select box then the second select box will display ALL the optiosn 1,2,3,4,5,6..

Is this possible?

I’ve seen it implemented on websites but not sure how to code this up?

Thanks,
Asha

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@simpson97Oct 30.2003 — I ran across this script at http://javascript.internet.com/

maybe with some mods it would work.

http://javascript.internet.com/

<html>

<head>

<script language="JavaScript" TYPE="text/javascript">

<!--

// Initialize class for Type and Style

function Type(id, type){

this.id = id;

this.type = type;

}

function Style(id, id_type, style){

this.id = id;

this.id_type = id_type;

this.style = style;

}

function init(sel_type, sel_style){

document.product.id_type.options[0] = new Option("[ Type ]");

document.product.id_style.options[0] = new Option("[ Style ]");

for(i = 1; i <= TypeArray.length; i++){

document.product.id_type.options[i] = new Option(TypeArray[i-1].type, TypeArray[i-1].id);

if(TypeArray[i-1].id == sel_type)

document.product.id_type.options[i].selected = true;

}

OnChange(sel_style);

}

function OnChange(sel_style){

sel_type_index = document.product.id_type.selectedIndex;

sel_type_value = parseInt(document.product.id_type[sel_type_index].value);

for(i = document.product.id_style.length - 1; i > 0; i--)

document.product.id_style.options[i] = null;

j=1;

for(i = 1; i <= StyleArray.length; i++){

if(StyleArray[i-1].id_type == sel_type_value){

document.product.id_style.options[j] = new Option(StyleArray[i-1].style, StyleArray[i-1].id);

if(StyleArray[i-1].id == sel_style) document.product.id_style.options[j].selected = true;

j++;

}

}

}

function start() {

// init(5, 31); // Initialize comboboxes by selected sel_type and sel_style



// Initialize Array's Data for Type and Style
TypeArray = new Array(
new Type(1, "Apparel"),
new Type(2, "Shoes"),
new Type(5, "Accessories"),
new Type(7, "Shirts")
);
StyleArray = new Array(
new Style(4, 1, "Apparel_1"),
new Style(7, 1, "Apparel_2"),
new Style(4, 1, "Apparel_3"),
new Style(41, 2, "Shoes_1"),
new Style(21, 2, "Shoes_2"),
new Style(17, 2, "Shoes_3"),
new Style(30, 5, "Accessories_1"),
new Style(27, 5, "Accessories_2"),
new Style(31, 5, "Accessories_3"),
new Style(33, 7, "Tee"),
new Style(34, 7, "Vee neck")
);

init(); // Default initialize comboboxes for Type and Style
}
window.onload = start;

//-->

</script>

</head>

<body bgcolor=ffcc99>

<form name="product">

<select name="id_type" size="1" style="width: 150px;" onChange="OnChange()">

</select>

<select name="id_style" size="1" style="width: 150px;">

</select>

</form>

</body>

</html>

Bob
×

Success!

Help @ashas 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 5.15,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...