View Full Version : Dynamic Dropdown Based on Date

Pigeon Poop
09-02-2010, 05:29 PM
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.

Here's what I need:

In September, the dropdown will display these prices

<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>

Old Pedant
09-02-2010, 07:36 PM
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

<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.

Philip M
09-02-2010, 07:43 PM
Something like this:-

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

<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);



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