Solved

use month from current date as default for select list

Posted on 2016-10-14
3
53 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 22

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Pass form variable from one web form to another 2 29
Filktering Alphabetically 8 28
Ahax pagination 9 32
Html Newline 7 20
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

790 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