1

Ok, I haven't been using JavaScript without JQuery for quite some time now... But, as coding goes, I have to do without it's power for a project where I can't be sure that JQuery is provided.

The thing I'm trying to do would be the following using JQuery:

$(document).ready(function() {
    $('#myDiv').append('<ul><li>a</li><li>b</li></ul>');
});

Now to the non-jquery thing, this is what I have and I really can't understand why it isn't working:

<html>
    <head>
        <script type="text/javascript">
            function load() {
                var s = '<ul><li>a</li><li>b</li></ul>'; 
                var element = document.getElementById("myDiv");
                element.innerHtml += s;
            }
        </script>
    </head>
    <body onload="load()">
        <div id="myDiv"></div>
    </body>
</html>

Thing is, nothing happens (the function gets called though). Could it be that the DOM isn't ready when load() is called?. I vaguely remember this code working in the firefox 2.x IE7 era ...

What would be the (a) right solution?

3 Answers 3

10

It's innerHTML not innerHtml. Notice the upper case HTML. Change that and it should work fine! Here's a working example.

By using innerHtml you are simply creating a new property on the element object and giving it the value of s.

Sign up to request clarification or add additional context in comments.

2 Comments

Oh ****, I totally missed that one, thank you! Need. More. coffee. :-)
No problem :) Always helps to get a few extra pairs of eyes to look over something! Glad I could help.
2
<html>
    <head>
        <script type="text/javascript">
            function load() {
                var s = '<ul><li>a</li><li>b</li></ul>';
                var element = document.getElementById("myDiv");
                element.innerHTML += s;
            }
        </script>
    </head>
    <body onload="load()">
        <div id="myDiv"></div>
    </body>
</html>

JS is case sensitive

Comments

0

I think you want element.innerHTML += s;.

Comments

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.