I have this code
$("#inputId").keyup(function(){
alert($("#inputId").val());
});
I know this is fairly basic(I’m not exactly sure what to google), but I’d like to know why that alert is always a null string.
EDIT-
For some reason, this wasn’t included.
What I meant was when I type in the field, the alert never shows anything- if this was imprecise, apologies, I meant the alert shows up, but it always shows a null string(so it should show what’s in the date field after a key has lifted, but does not) . That’s still happening with the fixed html below.
EDIT (follow up question)-
Cool, so I accepted one of the answers below. I’d like to make it so that regardless of what I type, the value comes up in the alert (not just when the entry is in the date format). How do I achieve this?
HTML CODE-
<div id = "container">
<input type = "date" id = "inputId" class = "inputClass"><br /></input>
<div id="deleteContainer"><span id="x">x</span></div>
</div>
I created a JSFiddle to test this and if you use and input type date then it should work.
Also changed $(‘#inputId’) into $(this) which saves an extra query and is faster.
As you can see in the Fiddle using the keyup with a date field (in Chrome) won’t return a value until the date is correct.
You can’t change the behavior of the date field, but you have two options.
‘change’ event in jQuery) and if the date is valid you get the value.
Here is a fiddle that demonstrates the change event instead of a keyup event.
Update Changed the fiddle to use input date