Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript How to auto select month from drop down using javascript

aousaja

New Coder
month-drop-down.PNG

I have this drop down and I want to auto select the current month on page load, means when ever the user gets to this page at any time the month drop down gets auto selected to current month, can any one help me in this regard I am new to javascript, here is my drop down html code:

HTML:
<select class="customSelect1 floating" id="month" name="month">
                    <option value="1">Jan</option>
                    <option value="2">Feb</option>
                    <option value="3">Mar</option>
                    <option value="4">Apr</option>
                    <option value="5">May</option>
                    <option value="6">Jun</option>
                    <option value="7">Jul</option>
                    <option value="8">Aug</option>
                    <option value="9">Sep</option>
                    <option value="10">Oct</option>
                    <option value="11">Nov</option>
                    <option value="12">Dec</option>
                </select>
 
You really do not need a dropdown to display the date.
Code:
    <p id="today"></p>
   <p id="mon">>
<script>
var today = new Date();

var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();

var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
document.getElementById('today').innerHTML=date +" "+ time;

//Change month to words
var me = today.toLocaleString('default', { month: 'long' });
document.getElementById('mon').innerHTML=me;

//OR

var date = today.getFullYear()+'-'+me+'-'+today.getDate();
document.getElementById('mon').innerHTML=date;
</script>
</body>
 
If you still want to use the dropdown:
Code:
<body>
<select class="customSelect1 floating" id="month" name="month">
    <option value="1">Jan</option>
    <option value="2">Feb</option>
    <option value="3">Mar</option>
    <option value="4">Apr</option>
    <option value="5">May</option>
    <option value="6">Jun</option>
    <option value="7">Jul</option>
    <option value="8">Aug</option>
    <option value="9">Sep</option>
    <option value="10">Oct</option>
    <option value="11">Nov</option>
    <option value="12">Dec</option>
</select>

<script>
var today = new Date();
var month = today.getMonth()+1;

document.getElementById("month").value = month;
</script>
</body>
 

New Threads

Buy us a coffee!

Back
Top Bottom