Print variable in div with javascript

Asked

Viewed 1,590 times

0

Hello doubt maybe silly but I am not able to solve personal, the following have a certain value javascript (variable) and need when the person enters the site print this variable in DIV? for example "hello world!" remembering that this variable will then change automatically then out of consideration by fixed the value .

<div><p >Imprimir aqui</p></div>

1 answer

3


Never forget to use window.onload in order to execute your code JavaScript after the HTML elements are ready to be manipulated.

I created an example for you to see change in real time.

window.onload = function() {
  var myvar = "Olá mundo";

  document.getElementById("mydiv").innerHTML = myvar;

  setInterval(function() {
    if (myvar == "Olá mundo") {
      myvar = "Mudando dinamicamente"
    } else {
      myvar = "Olá mundo"
    }

    document.getElementById("mydiv").innerHTML = myvar;
  }, 2000);
}
<div id="mydiv"></div>

I hope I’ve helped.

  • Thank you very much @Andrew Ribeiro guy you helped me many. Since I thank you.

Browser other questions tagged

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