KKR
KKR

Reputation: 67

The value to text box set using javascript not displaying on the textbox

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>

Upvotes: 1

Views: 870

Answers (4)

user13986534
user13986534

Reputation:

    <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.

Upvotes: 0

sonEtLumiere
sonEtLumiere

Reputation: 4572

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" />

Upvotes: 0

ray
ray

Reputation: 27285

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>.

Upvotes: 1

Asutosh
Asutosh

Reputation: 1828

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>

Upvotes: 0

Related Questions