How to use a Java type getText (). toString () in HTML with JavaScript?

1

How do I get what is typed in an HTML text box using JavaScript, as does getText().toString() in Java?

    
asked by anonymous 12.06.2016 / 22:48

1 answer

1

You can do this. But it has several other forms, it depends on what you want.

function getText() {
  input = document.getElementById('campo') //pega o elemento
  console.log(input.value); //pega o valor do elemento
}
<input type = "text" value = "texto aqui" id = "campo"/>
<input type = "button" value = "ok" onclick="getText();"/>

Documentation for getElementById() . The secret is the use of this browser DOM API function. It takes an element of the document being parsed by name. The name is defined by the id placed in tag HTML.

You can do this too, as I said, depending on the need.

<input type = "text" value = "texto aqui" id = "campo"/>
<input type = "button" value = "ok" onclick="console.log(document.getElementById('campo').value);"/>

You can do something more complex.

    
12.06.2016 / 22:58