hamish hamish - 4 months ago 8
Javascript Question

How to get text from input box to a span

I have got the HTML but I am not sure on how to get the input box into the span.
I tried using

document.getElementById
but that didn't work. Any help would be great!

Name:
<p>
<input type="text" name="fname" id="name">
<p>
<button id= "button" onclick="namejs">Go</button>
<p>
<span id= "namespan">Name</span>

Answer

You can try the following, this is plain Javascript Getting values you can use

document.getElementById("myspan").innerHTML="value";

For modern browsers

document.getElementById("myspan").textContent="value";

CODE

Name:
<p>
<input type="text" name="fname" id="name">
  <p>
<button id= "button" onclick="myFunction()">Go</button>
  <p>
  <span id="namespan">Name</span>

function myFunction() {
  var name = document.getElementById("name").value;
  document.getElementById("namespan").textContent=name;
}

Added a fiddle

Comments