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 09-02-2010, 05:29 PM   PM User | #1
Pigeon Poop
New to the CF scene

 
Join Date: Mar 2009
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
Pigeon Poop is an unknown quantity at this point
Dynamic Dropdown Based on Date

Javascript novice here looking to create a dynamic dropdown based on date.
It's a list of preset prices that will change each month to reflect a pro-rated system. Any thoughts/examples on how I would go about setting this up using javascript would be much appreciated.
Thanks.

Here's what I need:

In September, the dropdown will display these prices
Code:
<option value="100">Product 1</option>
<option value="150">Product 2</option>
<option value="200">Product 3</option>

In October, the dropdown will display these prices
<option value="50">Product 1</option>
<option value="100">Product 2</option>
<option value="150">Product 3</option>
Pigeon Poop is offline   Reply With Quote
Old 09-02-2010, 07:36 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,172
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Well, that's easy enough. But what happens in November? Do the prices become 0, 50, 100? I think I'll wait until November to buy. <grin/>

Seriously, tell us the *algorigthm* you are using to determine the prices.

By the by: In actuality, the dropdown will *NOT* "display these prices". The user can't see the prices as those <option>s are coded. So you *could* just us
Code:
<option value="1">Product 1</option>
<option value="2">Product 2</option>
<option value="3">Product 3</option>
and then have your "backend" code (PHP? ASP? whatever) do the matchup of product id to cost.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 09-02-2010, 07:43 PM   PM User | #3
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
Something like this:-

Code:
<select id = "sel1" onchange = "assignPrices()">>
<option value="A">Product 1</option>
<option value="B">Product 2</option>
<option value="C">Product 3</option>
</select>

<script type = "text/javascript">
function assignPrices() {
var price = 0;
var now = new Date();
var mth = now.getMonth();   // note that in Javascript months are 0-11
var val = document.getElementById("sel1").value;
if (mth == 8) {  // September
switch (val) {
case "A": price = 100; break;
case "B": price = 150; break;
case "C": price = 200; break;
}
}
if (mth == 9) {  // October
switch (val) {
case "A": price = 50; break;
case "B": price = 100; break;
case "C": price = 150; break;
}
}


alert ("The price is " + price);
}

assignPrices();

</script>

Note that JavaScript form validation only provides convenience for users, not security. This means that JavaScript should be used as an "enhancement", not as a requirement. So your form should not be dependent on JavaScript alone to assign the prices or perform your validation. Instead, whatever server-side language you use to process the form (PERL, ASP, PHP, etc.) should also perform the same validation. Otherwise, people will be able to bypass your validation (and even possibly inject malicious code) simply by disabling JavaScript.



This free kick is going to be a left-footed or a right-footed strike. - Football commentator

Last edited by Philip M; 09-02-2010 at 09:10 PM..
Philip M is offline   Reply With Quote
Reply

Bookmarks

Tags
date, dropdown, dynamic, list

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 07:53 PM.


Advertisement
Log in to turn off these ads.