Reputation: 19
I want to show the current month name in input box and in feature i put in my sql data as month
<script type="text/javascript">
const mnames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
const dat = new Date();
var month=( mnames[dat.getMonth()]);
document.getElementById('final').value = month ;
</script>
<input type="text" Id="final" name="final">
but now, no result is out put in box please correction my script
Upvotes: 0
Views: 232
Reputation: 1504
Well you can achieve the output by this code also and I recommend that you should load your script after you DOM is loaded , its good practice
<script type="text/javascript">
window.addEventListener('DOMContentLoaded', () => {
const mnames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
const dat = new Date();
var month=( mnames[dat.getMonth()]);
document.getElementById('final').value = month ;
});
</script>
<input type="text" Id="final" name="final">
Upvotes: 0
Reputation: 19
Put your script below your HTML. Currently, your script run before your HTML input load.
<input type="text" Id="final" name="final">
<script type="text/javascript">
const mnames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
const dat = new Date();
var month=( mnames[dat.getMonth()]);
document.getElementById('final').value = month ;
</script>
Upvotes: -1
Reputation: 20039
Try placing the script after input
element
<input type="text" Id="final" name="final">
<script type="text/javascript">
const mnames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
const dat = new Date();
var month=( mnames[dat.getMonth()]);
document.getElementById('final').value = month ;
</script>
Alternate Solution
Using toLocaleDateString
const date = new Date();
document.getElementById('final').value = date.toLocaleDateString('en-US', {
month: 'long'
});
<input type="text" Id="final" name="final">
Upvotes: 2