1

I want to populate html elements from a given string and this seems to not update the html inside the dynamic-content div:

In index.html:

<div id='dynamic-content'>
</div>

In index.js:

var dynamicContentNode = document.getElementById('dynamic-content');
dynamicContentNode.innerHTML = '<div><p>oh hai</p></div>';

Did I just not test this well?

Edit: I was assigning to innerHtml NOT innerHTML

Community
  • 1
  • 1
tarabyte
  • 14,444
  • 10
  • 58
  • 93
  • 3
    Can you expand on your usage of *fail*? – Marty Jan 27 '15 at 21:58
  • Looks perfectly fine. – Shomz Jan 27 '15 at 21:59
  • Updating question now. Basically, the innerHTML just doesn't seem set. – tarabyte Jan 27 '15 at 21:59
  • It's possible that your JavaScript is above your `
    `.
    – Marty Jan 27 '15 at 22:00
  • That [seems to work](http://jsfiddle.net/davidThomas/upv23yab/); so, are you correctly linking to your `index.js` file? And have you read this question: "[Why does jQuery or a DOM method such as `getElementById()` not find the element?](http://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element)" – David says reinstate Monica Jan 27 '15 at 22:00
  • @Marty, I certainly loaded the javascript in the `` tag accidentally, thanks. Still not working yet... – tarabyte Jan 27 '15 at 22:05

1 Answers1

2

Works perfectly fine.

Assuming that you load the JS file in the header, and thus the DOM is not ready (the div doesn't exist at the time the JS executes), so you can wrap the whole thing in a window.onload callback or load the JS file at the very end of the HTML body.

See the difference between the two examples below reflecting what actually happens if you load the JS before and after the HTML.


Non-working version:

<script>var dynamicContentNode = document.getElementById('dynamic-content');
dynamicContentNode.innerHTML = '<div><p>oh hai</p></div>';</script>
<div id='dynamic-content'>
</div>

Working version:

<div id='dynamic-content'>
</div>
<script>var dynamicContentNode = document.getElementById('dynamic-content');
dynamicContentNode.innerHTML = '<div><p>oh hai</p></div>';</script>

Non-working version made working using the onload callback:

<script>
  window.onload = function(){
    var dynamicContentNode = document.getElementById('dynamic-content');
    dynamicContentNode.innerHTML = '<div><p>oh hai</p></div>';
  }
</script>
<div id='dynamic-content'>
</div>
Shomz
  • 36,161
  • 3
  • 52
  • 81
  • After adding the non-working version, your working version doesn't work. I've never seen this code snippet feature before. Very cool. Not sure how I missed it. – CoderDennis Jan 27 '15 at 22:05
  • Snippets are quite young here. What do you mean it doesn't work? The first one is supposed to fail, while the other two work fine. – Shomz Jan 27 '15 at 22:09
  • None of them display anything in the results section when I try them. – CoderDennis Jan 27 '15 at 22:11
  • Did you hit the run button? – Shomz Jan 27 '15 at 22:11
  • Yes. I'm hitting the run button. The first version of your answer worked just fine. Your javascript is no longer within the js portion of the snippet. Could that be the problem? – CoderDennis Jan 27 '15 at 22:14
  • I've never seen that happen with a snippet, even after a refresh? Anyway, that's what the problem with your code was, try implementing one of the solutions and see if it works for you. – Shomz Jan 27 '15 at 22:16
  • Chrome console displays this error: "The XSS Auditor refused to execute a script in 'http://stacksnippets.net/js' because its source code was found within the request. The auditor was enabled as the server sent neither an 'X-XSS-Protection' nor 'Content-Security-Policy' header." – CoderDennis Jan 27 '15 at 22:20
  • Did you try to load an actual file inside the snippet? – Shomz Jan 27 '15 at 22:24
  • What do you mean? That's what they are for. Did you figure out what were you doing from with them? – Shomz Jan 27 '15 at 22:35
  • Yes, question updated. I was created a new property, `innerHtml`, by accident. The method should've been, `innerHTML` – tarabyte Jan 27 '15 at 22:59