Solved

use month from current date as default for select list

Posted on 2016-10-14
3
25 Views
Last Modified: 2016-10-15
I have this ddl fiddle

http://jsfiddle.net/L5z6e2ga/

In the select list i'm defaulting to october on page load llike this:  
<option selected="selected" value="10/01/2016">October</option>

Instead of hard coding it like this. How do I use date function and look up today's month and use that month to then set the selected month on the input select.

So if I launch this fiddle this month (which is October) it will default to October, but if I launch it next month. I will launch with November by default.
0
Comment
Question by:maqskywalker
3 Comments
 
LVL 21

Accepted Solution

by:
Kim Walker earned 250 total points
ID: 41844627
You've selected jQuery as a tag for your question so I assume you want to do this on the front end after the page loads. Correct?

First you need to make it easier to select the correct month from the select list by adding unique ids to each option.
                       <select id="DDLMonth1" name="DDLMonth1" class="DDLMonth1">
                           <option id="month6" value="07/01/2016">July</option>
                           <option id="month7" value="08/01/2016">August</option>
                           <option id="month8" value="09/01/2016">September</option>
                           <option id="month9" value="10/01/2016">October</option>
                           <option id="month10" value="11/01/2016">November</option>
                           <option id="month11" value="12/01/2016">December</option>
                           <option id="month0" value="01/01/2017">January</option>
                           <option id="month1" value="02/01/2017">February</option>
                           <option id="month2" value="03/01/2017">March</option>
                           <option id="month3" value="04/01/2017">April</option>
                           <option id="month4" value="05/01/2017">May</option>
                           <option id="month5" value="06/01/2017">June</option>
                       </select>

Open in new window

Then you can paste the following code at the bottom of the page to determine the current month and select it. Javascript dates use 0 (zero) for January and 11 for December.
<script type="text/ecmascript">
    var d = new Date();
    $('#month'+d.getMonth() ).prop('selected',true);
</script>

Open in new window

You could also use the values as a selector instead of adding the ids but then you'd have to deal with the leading zero.
0
 
LVL 35

Assisted Solution

by:Miguel Oz
Miguel Oz earned 250 total points
ID: 41844748
There is no need for ids (Kim's comment) if you only use month values as shown below:
 <select id="DDLMonth1" name="DDLMonth1" class="DDLMonth1">
    <option value="7">July</option>
    <option value="8">August</option>
    <option value="9">September</option>
    <option value="10">October</option>
    <option value="11">November</option>
    <option value="12">December</option>
    <option value="1">January</option>
    <option value="2">February</option>
    <option value="3">March</option>
    <option value="4">April</option>
    <option value="5">May</option>
    <option value="6">June</option>
</select>

Open in new window


Your start up javascript  is:
<script type="text/javascript">
        var d = new Date();
	$('#DDLMonth1').val(d.getMonth()+1); //Set selected value
</script>

Open in new window


Note: The Jquery val function is very handy.
1
 
LVL 1

Author Closing Comment

by:maqskywalker
ID: 41845048
thanks.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
This article discusses four methods for overlaying images in a container on a web page
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now