Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

use month from current date as default for select list

Posted on 2016-10-14
3
Medium Priority
?
79 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 22

Accepted Solution

by:
Kim Walker earned 1000 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 36

Assisted Solution

by:Miguel Oz
Miguel Oz earned 1000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

618 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