1

Trying to set the value of a text box using javascript the value displays on the web page but doesn't seem to display in the text box Below is the code snippet. Appreciate some help.

var d = new Date();
var dd = d.getDate();
var mm = d.getMonth()+1; 
var yyyy = d.getFullYear();
today = yyyy+""+mm+""+dd;
document.getElementById("demo").innerHTML = d.getTime();
document.getElementById("demotime").innerHTML = today;
document.getElementById("demotime").value = today;
<html>
    <body>
      <h2>JavaScript getTime()</h2>
      <p id="demo"></p>
      <p id="demotime"></p>
      <form>
        <input type="text" id="demotime" />
      </form>
   </body>
 </html>

0

4 Answers 4

1

You have more than one element with that ID. IDs must be unique within the document. getElementById will likely return the first one, which is not the input. You’re setting .value on the <p>.

Sign up to request clarification or add additional context in comments.

Comments

0

var d = new Date();
var dd = d.getDate();
var mm = d.getMonth()+1; 
var yyyy = d.getFullYear();
today = yyyy+""+mm+""+dd;
document.getElementById("demo").innerHTML = d.getTime();
document.getElementById("pdemotime").innerHTML = today;
document.getElementById("demotime").value = today;
<h2>JavaScript getTime()</h2>
<p id="demo"></p>
<p id="pdemotime"></p>
<form>
<input type="text" id="demotime" />
</form>

Comments

0

You need to name elements with diferent id name

var d = new Date();
var dd = d.getDate();
var mm = d.getMonth()+1; 
var yyyy = d.getFullYear();
let today = yyyy+""+mm+""+dd;

document.getElementById("demo").innerHTML = d.getTime();
document.getElementById("demotime").innerHTML = today;
document.getElementById("demotime2").value = today;
<p id="demo"></p>
<p id="demotime"></p>
<form>
<input type="text" id="demotime2" />

Comments

0
    <html>
    <body>
    <h2>JavaScript getTime()</h2>
    <p id="demo"></p>
    <p id="timeDemo"></p>
    <form>
    <input type="text" id="demotime" />
    </form>
    <script>
    var d = new Date();
    var dd = d.getDate();
    var mm = d.getMonth()+1; 
    var yyyy = d.getFullYear();
    today = yyyy+"-"+mm+"-"+dd;
    document.getElementById("demo").innerHTML = d.getTime();
    document.getElementById("demotime").innerHTML = today;
    document.getElementById("demotime").value = today;
    </script>
    </body>
    </html>

You have use same Id for two different attributes. I have changed the id of p tag. Hope you find your desire result. You may also check the attached code snippet.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.