0

I'm trying to get the inside of a div to change. after five seconds the div content stays the same. and I'm stuck..

<script type="text/javascript">
  var $div = $("#tiedotteet");

  var timer = setInterval( function() {
   $div.html("<h1>Test</h1>");
  }, 5000);
</script>

<div id="tiedotteet">
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Pa2
  • 11
  • 2
  • Try to add the div before the script in your html. – JJWesterkamp Jan 13 '19 at 19:04
  • Put the `div` before the `script` or use a document.ready event handler. Also note that the interval is pretty redundant, as after the first iteration you won't see any change. Perhaps a `setTimeout()` would be more apt. – Rory McCrossan Jan 13 '19 at 19:11

1 Answers1

2

DOM elements should be rendered before script. Check this https://stackoverflow.com/a/24070373/5232486

<div id="tiedotteet"></div>
<script type="text/javascript">
  var $div = $("#tiedotteet");

  var timer = setInterval(function () {
    $div.html("<h1>Test</h1>");
  }, 5000);
</script>
Phanindra
  • 329
  • 2
  • 11