Stone
Stone

Reputation: 43

To print along with the textbox in javascript

I'm learning JavaScript.
I want to print the data that user writes in the textbox.The operation should go hand-in-hand i.w if he writes 2 it should be displayed 2 first then 23 then displayed 23 and so on.

function onChange() {
  document.getElementById("display").innerHTML = document.getElementById("name").value;
}
<html>

<head>
</head>

<body>

  Enter Your Name:<input type="text" id="name" onkeyup="this.onChange();"> 

  Show::<span id="display"></span>
</body>

</html>


Upvotes: 1

Views: 35

Answers (2)

Orelsanpls
Orelsanpls

Reputation: 23515

  • There is no this before onchange.

  • You can use this.value to directly access the value and use it instead of targeting the element again using getElementById.

function onChange(value) {
  document.getElementById('display').innerHTML = value;
}
<html>

<head>
</head>

<body>

  Enter Your Name:<input type="text" id="name" onkeyup="onChange(this.value)"> Show::

  <span id="display"></span>
</body>

</html>

Upvotes: 2

Mr. Roshan
Mr. Roshan

Reputation: 1805

Try this:-

 function onChange() {
          document.getElementById("display").innerHTML = document.getElementById("name").value;
        }
  <html>
        
        <head>
        </head>
        
        <body>
        
          Enter Your Name:<input type="text" id="name" onkeyup="onChange()"> Show::
        
          <span id="display"></span>
        </body>
        
        </html>

Upvotes: 1

Related Questions