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 06-28-2012, 02:42 PM   PM User | #1
Riku
New Coder

 
Join Date: Feb 2012
Location: Finland
Posts: 57
Thanks: 3
Thanked 9 Times in 9 Posts
Riku is an unknown quantity at this point
Use JS to check 2 dropdowns and show div based on that data

Hello,

Here is my previous post

http://www.codingforums.com/showthread.php?t=266513

It's in HTML and CSS section so decided to post my follow up question based on the answear on this JavaScript section.

So how can I get JavaScript to check the values of my 2 options and show right div based on those two?

Lets say I got basic dropdown as described in my previous post and user would choose New York and Los Angeles and the div that has the list would have an ID of "NYC-LOS" or something like that.

How can I use JavaScript to scan the selections and show the right content?

Any help is welcome, just to get me started!

-Riku
Riku is offline   Reply With Quote
Old 06-28-2012, 04:31 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,043
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by Riku View Post

How can I use JavaScript to scan the selections and show the right content?
Like this - but you will need a lot of divs to cover all the cities in the USA! And are there not very many (hundreds?) of gas stations between New York and Los Angeles? Surely a driver simply fills up his tank when he needs to. That will depend on the MPG of the car and the speed driven. What use is it to know that there are gas stations hundreds of miles away from where you are now?

Code:
<html> 
<head> 

<style = "text/css">
.mydivhide {display:none;}
.mydivshow {display:block;}
</style>
<body>

<form>

FROM <select id = "fromcity">
<option value = "NYC" >New York</option>
<option value = "BOS">Boston</option>
</select>

TO <select id = "tocity">
<option value = "LOS" >Los Angeles</option>
<option value = "SAN">San Francisco</option>
</select>

<input type = "button" value = "Show Info" onclick = "showStuff()">
</form>

<div id = "container">
<div id = "NYC-LOS" class = "mydivhide" >Info about New York to Los Angeles</div>
<div id = "NYC-SAN" class = "mydivhide" >Info about New York to San Francisco</div>
<div id = "BOS-LOS" class = "mydivhide" >Info about Boston to Los Angeles</div>
<div id = "BOS-SAN" class = "mydivhide" >Info about Boston to San Francisco</div>
</div>

<script type = "text/javascript">

function showStuff() {
var fr =  document.getElementById("fromcity").value;
var to = document.getElementById("tocity").value;

var d  = document.getElementById('container').getElementsByTagName('div');
var len = d.length;
for (var i = 0; i <len; i++) {
d[i].className = "mydivhide";
}

var div2show = fr + "-" + to;
document.getElementById(div2show).className = "mydivshow";

}

</script>

</body>
</html>
My thanks and commiserations to the other contestants, and to you for listening. - Quizmaster, BBC Radio 4
__________________

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; 06-28-2012 at 05:30 PM..
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
Riku (06-28-2012)
Old 06-28-2012, 05:39 PM   PM User | #3
Riku
New Coder

 
Join Date: Feb 2012
Location: Finland
Posts: 57
Thanks: 3
Thanked 9 Times in 9 Posts
Riku is an unknown quantity at this point
MAny many thanks to you!

I'm not on plannin to do anything even related to USA or gas stations, that example was just the easiest way to demostrate the feature I'm looking for. The thing I'm building is related to Finnish stuff that I can't translate or explain in english so I made up example of something else that I can modify to fit my needs, and this is just perfect!

Respect (Y)

-Riku
Riku 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 11:02 PM.


Advertisement
Log in to turn off these ads.