How to use a kind of gettext(). toString() from Java in HTML with Javascript?

Asked

Viewed 346 times

1

How to get what is typed in an HTML text box using Javascript, as well as getText().toString() in Java?

  • 1

    Can you explain what you mean by "HTML text box"? You can display the HTML?

  • An input in my case = <input type="text">

  • 1

    var texto = document.querySelector('input').value; <- this reads the value of the first input you find on the page, the method accepts CSS selectors, in this case use a simple: input. That’s what you’re looking for?

1 answer

1


You can do this. But there are several other ways, depending 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 of 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 set by id placed in the tag HTML.

Can do so too, as I said, depends on the need.

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

I put in the Github for future reference.

Can do something more complex.

  • Yes that’s what I was looking for, thank you very much!

Browser other questions tagged

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