Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-10-2012, 12:00 PM   PM User | #1
topdown.me
New Coder

 
Join Date: Jul 2011
Posts: 31
Thanks: 2
Thanked 0 Times in 0 Posts
topdown.me is an unknown quantity at this point
Change [Select 2] options when [Select 1] option is changed

Hello.
I have 2 selects in a form:
Select 1
-food
-drinks
Select 2
-hamburger
-cheetos
-orange juice
-cola

I want to do that when I choose "FOOD" in select 1, I get the options: Hamburger and cheetos, and when I pick "DRINKS" in select 1,
select 2 will change to orngae juice and cola.

How I can do that? (need to work in all browsers)
__________________
League of Legends
topdown.me is offline   Reply With Quote
Old 11-10-2012, 12:33 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
I hope this is not homework! Here you are:-

Code:
<html>
<head>

<script type = "text/javascript">

function setOptions(chosen) {
var selbox = document.getElementById("myform").opttwo;
 
selbox.options.length = 0;
if (chosen == "0") {
selbox.options[selbox.options.length] = new Option('Please select one of the options above first',' ');
}

if (chosen == "1") {
selbox.options[selbox.options.length] = new Option('Hamburger     ','Hamburger');
selbox.options[selbox.options.length] = new Option('Cheetos','Cheetos');
}

if (chosen == "2") {
selbox.options[selbox.options.length] = new Option('Orange Juice     ','Orange Juice');
selbox.options[selbox.options.length] = new Option('Cola','Cola');
}

}

</script>

</head>
<body>

<form id="myform">

<select name="optone" 
onchange="setOptions(document.getElementById('optone').selectedIndex);">
<option value="0" selected="selected"> Please Select </option>
<option value="1"> Food </option>
<option value="2"> Drink </option>
</select>
<br> <br>
<select name="opttwo">
<option value=" " selected="selected">Please select one of the options above first</option>
</select>
<br><br>
<input type="button" name="go" value="Click To Alert Value Selected" onclick="alert(document.getElementById('myform').opttwo.value)">

</form> 

</body>
</html>
"I have not failed. I've just found 10,000 ways that won't work. " - Thomas Edison
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 11-10-2012 at 12:44 PM.. Reason: Improved
Philip M is offline   Reply With Quote
Old 11-10-2012, 12:43 PM   PM User | #3
topdown.me
New Coder

 
Join Date: Jul 2011
Posts: 31
Thanks: 2
Thanked 0 Times in 0 Posts
topdown.me is an unknown quantity at this point
Quote:
Originally Posted by Philip M View Post
I hope this is not homework! Here you are:-

Code:
<html>
<head>

<script type = "text/javascript">

function setOptions(chosen) {
var selbox = document.myform.opttwo;
 
selbox.options.length = 0;
if (chosen == "0") {
selbox.options[selbox.options.length] = new Option('Please select one of the options above first',' ');
}

if (chosen == "1") {
selbox.options[selbox.options.length] = new Option('Hamburger     ','Hamburger');
selbox.options[selbox.options.length] = new Option('Cheetos','Cheetos');
}

if (chosen == "2") {
selbox.options[selbox.options.length] = new Option('Orange Juice     ','Orange Jiuce');
selbox.options[selbox.options.length] = new Option('Cola','Cola');
}

}

</script>

</head>
<body>

<form name="myform">

<select name="optone" 
onchange="setOptions(document.myform.optone.options[document.myform.optone.selectedIndex].value);">
<option value="0" selected="selected"> Please Select </option>
<option value="1"> Food </option>
<option value="2"> Drink </option>
</select>
<br> <br>
<select name="opttwo">
<option value=" " selected="selected">Please select one of the options above first</option>
</select>
<br><br>
<input type="button" name="go" value="Click To Alert Value Selected" onclick="alert(document.myform.opttwo.options[document.myform.opttwo.selectedIndex].value);">

</form> 

</body>
</html>

"I have not failed. I've just found 10,000 ways that won't work. " - Thomas Edison
This is not Homework lol, I just used it as example of what I want so you can understand me easier

btw, its not helping me since I need to use PHP to loop all my options, wich is impossible to use in a javascript script.
Help?
__________________
League of Legends
topdown.me is offline   Reply With Quote
Old 11-10-2012, 12:50 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by topdown.me View Post
This is not Homework lol, I just used it as example of what I want so you can understand me easier

btw, its not helping me since I need to use PHP to loop all my options, wich is impossible to use in a javascript script.
Help?
Oh, sorry I spoke! I don't know much about PHP.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 11-10-2012, 07:13 PM   PM User | #5
topdown.me
New Coder

 
Join Date: Jul 2011
Posts: 31
Thanks: 2
Thanked 0 Times in 0 Posts
topdown.me is an unknown quantity at this point
Need help here.
Something that can be good for me too:
When an option is chosen in Select 1, Select 2 shows up. when another option, Select 3 shows up, etc..
Easier?
__________________
League of Legends
topdown.me is offline   Reply With Quote
Old 11-10-2012, 09:15 PM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by topdown.me View Post
Need help here.
Something that can be good for me too:
When an option is chosen in Select 1, Select 2 shows up. when another option, Select 3 shows up, etc..
Easier?
Try

http://www.javascriptkit.com/script/...plecombo.shtml
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 11-11-2012, 02:28 AM   PM User | #7
minder
Banned

 
Join Date: Oct 2012
Posts: 81
Thanks: 0
Thanked 4 Times in 4 Posts
minder can only hope to improve
The op has x-posted in another thread.

You said it needs to work in all browsers, so you can't use javascript because it won't work in browsers that do not support javascript or have it disabled for some reason.

If you want to guarantee it to work in all browsers then have a look at a starting point php solution in your other thread on this topic

http://www.codingforums.com/showpost...17&postcount=7

If it doesn't have to work in all browsers, then by all means use javascript
minder is offline   Reply With Quote
Old 11-11-2012, 08:02 AM   PM User | #8
topdown.me
New Coder

 
Join Date: Jul 2011
Posts: 31
Thanks: 2
Thanked 0 Times in 0 Posts
topdown.me is an unknown quantity at this point
Quote:
Originally Posted by Philip M View Post
Thank you, should be good.
Edit: no, its not good.. I cant loop my options.. \:
__________________
League of Legends

Last edited by topdown.me; 11-11-2012 at 08:52 AM..
topdown.me is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:58 AM.


Advertisement
Log in to turn off these ads.