0

Not sure if it is weird or simply I am stupid, but I am having an issue in JavaScript innerHTML. Ok, I hve an empty DIV in my body tag. What I want is to write an unordered list in it. So, I made this:

<script>
var bla = document.getElementById("bla");
bla.innerHTML = "<ul><li>LOL</li></ul>";
</script>

<div id="bla">

</div>

Hmm nothing is appearing in the DIV.. :(

2 Answers 2

3

id='bla' was not found because you script above the id and not use any function

change like this

<div id="bla">

</div>

<script>
var bla = document.getElementById("bla");
bla.innerHTML = "<ul><li>LOL</li></ul>";
</script>

or

<script>
window.onload=function(){var bla = document.getElementById("bla");
bla.innerHTML = "<ul><li>LOL</li></ul>";
 };
</script>

<div id="bla">

</div>
Sign up to request clarification or add additional context in comments.

2 Comments

Really? this looks like it should work. Did you try running the code in the JavaScript console as a sanity check? var bla = document.getElementById("bla"); bla.innerHTML = "<ul><li>LOL</li></ul>"; Are you sure the code is actually being called and that there isn't another onload callback overwriting this one? Put an alert or something in that onload to convince yourself that it's actually the innerHTML that's the problem and not that your callback is being hijacked.......
now it works lol, I forgot to add (); in the function lo..thank
0
<div id="bla">

</div>
<script>
var bla = document.getElementById("bla");
bla.innerHTML = "<ul><li>LOL</li></ul>";
</script>

1 Comment

if you will write/execute the script before <div id="bla"></div> then page will not find any id "bla" with this name as div with id bla has not uploaded yet. so either use above one or <script> window.onload=function(){var bla = document.getElementById("bla"); bla.innerHTML = "<ul><li>LOL</li></ul>"; }; </script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.