jQuery get value of input datetime-local
jQuery get value of input datetime-local
I need to get the value from input datetime-local, but the variable is empty, whereas everything is ok with text inputs. Here is code:
<script>
var datetimeval = $("#datetime").val();
var textval = $("#text").val();
</script>
<input id='text' type='text' />
<input id='datetime' type='datetime-local' />
Could you tell me please, how to fix it?
Have you debugged your code? What errors do you get?
– j08691
Aug 18 '14 at 13:35
you wrap the code inside document.ready learn.jquery.com/using-jquery-core/document-ready
– Balachandran
Aug 18 '14 at 13:37
I cannot get the datetime even so: jsfiddle.net/t89q3cfc/3
– egorfadeev
Aug 18 '14 at 14:01
2 Answers
2
Try this
<input id='text' type='text' value="test"/>
<input id='datetime' type='datetime-local' />
<script>
var datetimeval = $("#datetime").val();
var textval = $("#text").val();
alert(textval);
alert(datetimeval);
</script>
Demo: Fiddle
Note: I gave default value to text input textbox
Thanks for reply. But I cannot get the datetime even so: jsfiddle.net/t89q3cfc/3
– egorfadeev
Aug 18 '14 at 14:01
@egorfadeev: you want to get current date time in textbox or the value of textbox that user has entered?
– Sid M
Aug 18 '14 at 14:05
I need to get the value from <input id='datetime' type='datetime-local' /> when I click on button
– egorfadeev
Aug 18 '14 at 14:10
@egorfadeev : then what's the problem i get the value of your textbox after i enter both date and time.
– Sid M
Aug 18 '14 at 14:23
You should set its value of all parts, including y-m-d h:m, especially "h:m". It's ok for my case.
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
you need to add the script after adding the input elements
– Arun P Johny
Aug 18 '14 at 13:33