Print variable in div with javascript

0

Hello, I'm not sure what to do, but I'm not sure how to do this. for example "Hello world!" remembering that this variable will then automatically change out of consideration for a fixed value.

<div><p >Imprimir aqui</p></div>
    
asked by anonymous 02.09.2016 / 01:10

1 answer

3

Never forget to use window.onload to be able to run your JavaScript code after HTML elements are ready to be manipulated.

I've created an example for you to see the 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 have helped.

    
02.09.2016 / 01:22