Html text box value to be received by javascript variable

Asked

Viewed 5,508 times

0

Hello, I’m learning javascript and wanted to know how to do what is typed in the HTML text box printed on the screen in javascript Document.write.

  • Do you have any code to show? This can help a lot to answer.

3 answers

3

You can do it this way, but using the document.write will make you rewrite your page, making your controls disappear.

function writeItDown(btn){
  var suaVariavel = document.getElementById("txtInput").value;
  document.write(suaVariavel);
}
<input type="text" id="txtInput" /> <button id="myBtn" onclick="writeItDown(this);" >Write</button>

The best option would be to use an html element as a span and write within it through the property innerHTML of the element. thus:

function escrever(btn){
  var suaVariavel = document.getElementById("txtInput").value;
  document.getElementById('meuTextoVaiAqui').innerHTML = suaVariavel;
}
<input type="text" id="txtInput" /> <button id="myBtn" onclick="escrever(this);" >Write</button>
<br/><span id="meuTextoVaiAqui"></span>

  • THANK YOU ANDRE PAULO.

0

You can always do something like that:

HTML:

<p>
    //o resultado vai cair aqui
</p>

//com um input
<input type="text" onkeyup="changeResult(this)" />

//ou com uma textarea
<textarea onkeyup="changeResult(this)"></textarea>

JS:

var changeResult = function(element)
{
   document.getElementsByTagName("p") = element.value; 
}

It is possible that there is some error because I have not tested but the logic is this

  • THANKS, SIMON WE READ.

  • If you have the correct answer mark as correct please, or say what failed

0

Library

<script src="http://code.jquery.com/jquery-1.8.3.min.js" type="text/javascript"></script>

Script

$(document).ready(function() {
      $('.class').keyup(function (e) {
       var v1 = (document.getElementById("v1").value);
       $('#v2').text(v1);
   });
});

HTML

<input type="text" class="class" name="v1" id="v1" size="10" /> 
<span id="v2"> </span>>
  • THANK YOU LEO CARACCIOLO.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.