How to assign a java variable to Document.getElementBy Id

Asked

Viewed 1,190 times

-1

I have the following code below, where I want to store the value of the variable contained in document.getElementById, which comes from input, and which should be "text". This line of programming only prints the initial input value on the screen, but does not store it. I tried to associate to var note, and gave a print command to see if it really was registered, it should appear written the value of the input twice, but only appears once. I wish to use was new variable, for another purpose. What would be wrong?

<input id="text">
......
........
<script>
  var speaker = new RobotSpeaker();
  function talk() {
    speaker.speak("pt", document.getElementById("text").value);///esta linha imprime o texto FALADO, na tela
    var nota = document.getElementById("text").value); //salvaria em nota o valor de text??
    console.log(nota.value); // Recuperando o valor:
    window.document.write("nota");///imprime na tela de novo???
  }
  • Where is the Java variable mentioned in the title?

2 answers

0

I don’t know what mistake you’re making, but try it this way:

function talk() {
   speaker.speak("pt", document.getElementById("text").value); // ve se o problema não é esta linha, comenta-a para confirmar.
   var nota = document.getElementById("text").value;
   console.log(nota);
   window.document.write(nota);
}

Note that the.log console only shows the value of the variable in the browser console, Document.write prints on the page.

  • What has changed in the code?

  • 1

    I think your second line is going to fail

  • var note = Document.getElementById("text"). value) had an extra ")" in the console line.log, as the note was already value, making note.value will give error. At last write("note") will write note instead of value.

  • @Arturotemplário to delete the comments had left there a little that was on the other line

0

I didn’t really understand the purpose of your question, and it seems to me that you don’t have well-established concepts of attribution and use of variables. Your code also has some errors:

var nota = document.getElementById("text").value);

Here, the final closing parenthesis is left over, and will probably cause errors sooner or later. It is necessary to remove it:

var nota = document.getElementById("text").value;

Here, the input value with the id "text" has already been stored in the variable nota

console.log(nota.value);

The note value is a string, so you don’t have to put nota.value. Just like that is enough:

console.log(nota);

Here’s the part I don’t understand what you mean:

window.document.write("nota");

If you wanted to write the note variable on your screen, just take out the quotes.

window.document.write(nota);

If you put the quotes, it will be interpreted as a string with the value "note", and will literally write this on the screen.

  • OK grateful, I’ll be making the changes and informing.

Browser other questions tagged

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