Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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>

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,901
    Thanks
    79
    Thanked 4,421 Times in 4,386 Posts
    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.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    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.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •