0

I'm trying to add a div with some text before another div in the document. Here's my script in a nutshell, assume init() gets called when onload page:

       <head>
            <script type="text/javascript">
                function init () {
                       var div = document.createElement('div').className = 'title';
                       div.innerHTML = 'Hello';
                       var reference = document.getElementById('content');
                       document.body.insertBefore(div, reference );            
                }
            </script>
        </head>
        <body>
            <div id="content">blah blah</div>
            <br /><br />
        </body>

I get a type mismatch error on document.body.insertBefore(div, reference), Can someone please let me know what i'm doing wrong?

1
  • Your assignment of div will leave the value of that variable as the string "title". Commented Mar 28, 2013 at 20:17

1 Answer 1

1

Try

function init () {
     var div = document.createElement('div');
     div.className = 'title';
     div.innerHTML = 'Hello';
     var reference = document.getElementById('content');
     document.body.insertBefore(div, reference );            
}

You were setting the div to the string title I think (never checked)

Yep here you go

http://jsfiddle.net/FxFzc/

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

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.