Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

319
Views
Get value of input type=date as a string

I want to extract the value from date field, but it doesn't work. I add the input this way:

var question0  = "<div id='0'><p>Please, enter the date: </p><br>" 
                       + "<input type=\"date\" id=\'contractdate\'></input><br></div>";

Here is how I tried to receive the value:

var text_Contract_Date     = document.getElementById('contractdate').value;
//tried the code below, but didn't work
// var text_Contract_Date  = document.getElementById('contractdate').valueAsDate;
// var text_Contract_Date  = new Date(document.getElementById('contractdate').valueAsDate);

So, I want to get the value from input as a string, using pure JavaScript, because then it will be used to fill in the document.

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Try this

var dateEntered = new Date(text_Contract_Date);
about 4 years ago · Santiago Trujillo Report

0

There is not enough information to resolve the issue, so I can only guess that you are probably inserting your variable into the DOM the wrong way. If I call document.body.append(question0), only text is shown and not the tags. Try moving content of question0 variable to your html file, then add onchange handler to your input, and also modify your .js file like below

function handleChange(event){
  // here you can do whatever you want with the value of the input
  alert(event.target.value)
}
<input type="date" id='contractdate' onchange="handleChange(event)"></input>

If you desperately want to create your HTML inside of Javascript, you have to do this like this:

// create div and assign id to it
const myDiv = document.createElement("div")
myDiv.id = '0'

// create p and set its contents
const myP = document.createElement("p")
p.textContent = "Please, enter the date: "

// create input, assign id to it and set its type to date
const myInput = document.createElement("input")
myInput.id = 'contractdate'
myInput.type = "date"

// put everything in your document
myDiv.appendChild(myP)
myDiv.appendChild(myInput)
document.body.appendChild(myDiv)
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!