Erfan Ahmed Emon Erfan Ahmed Emon - 7 months ago 12
Javascript Question

Dynamic selection of HTML select tag in laravel5.0

Let's have a select tag of months of a year

<label name="month">Select Month</label>
<select class="form-control">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
</select>


How can I set the current month number dynamically ? e.g. If it is May then '5' will be selected automatically. However user can also select a month manually with the help of drop down option.I want to use it in my laravel5.0 application.

Answer

Basically, you need use JavaScript getMonth() Method.

If you do not use JQuery, try

var date = new Date();
var month = date.getMonth + 1
document.getElementByClass("form-control").value = month;

Thanks for the comment of Erfan, we need +1 for the value of month.

Note: In getMonth(), 0=January, 1=February etc.