I am trying to take form input of type date and display it on page. Nothing is being displayed when
function checkin(){
var date = document.getElementById("cInDate").value;
var dateArray = date.split("/");<!-- puts date into dateArray->
var s = document.getElementById("dateI");
s.textContent = dateArray[1]"th of July";<!-- shows 2nd index of dateArray on page->
}
<form id="myForm">
<!-- collects checkin date form input -->
Check In Date: <input type="date" name="checkin" id="cInDate">
<button onclick="checkin()" >Submit</button>
</form>
<h1 class="al">Hotel Room Availability</h1>
<p class="al">You want to check in on
<span id="dateI"></span> <!-- shows checkin date -->
</p>
For solve your problem you can use this code source :
If input type date is supported, it returns a value in ISO 8601 format yyyy-mm-dd. Note that there is no time zone associated with it. If parsed by the Date constructor, most modern browsers will return a Date for date based on midnight, UTC.
If the host is in a timezone west of Greenwich, its local date values be offset for the time zone so the date will be for the previous day. If you want the date treated as local, you must manually parse it:
However, not all browsers in use support input type date, so you should deal with that.
The errors were being caused by your comments not being formatted properly.
<!-- content -->
define an HTML comment, but doesn't work in Javascript. For JS, use//Content
or/* Content */
.Also be careful about your JS syntax. Note the
+
I added in the last line of your function, as well as the closing</input>
and</p>
tags in your HTML. *As RobG has noted this isn't strictly necessary, but it helps me at least.Edit: You also need to change
.split('/')
to.split('-')
, because as you can see from the console, the date is separated by-
s