How to assign a java variable to document.getElementBy Id

0

I have the following code, where I want to store the value of the variable contained in document.getElementById , which comes from the input, and which should be "text". This line of programming only prints the value of the initial input on the screen but does not store it. I tried to associate it with the var note, and I gave a print command to see if it really was registered, it should be written the input value twice, but only appears once. I wish to use it 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???
  }
    
asked by anonymous 09.05.2017 / 18:14

2 answers

0

I did not quite understand the purpose of your question, and it seems to me that you have not well established the concepts of assignment 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. You must remove it:

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

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

console.log(nota.value);

The note value is a string, so you do not need to nota.value . Just this is enough:

console.log(nota);

Here is the part that did not understand what you intended:

window.document.write("nota");

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

window.document.write(nota);

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

    
09.05.2017 / 18:24
0

I do not know what the error you are having, but try this:

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 console.log only shows the value of the variable in the browser console, the document.write is printing on the page.

    
09.05.2017 / 18:19