Jump to content



Recommended Posts


I have this simple code

        function addTable() {
            document.getElementById("one").insertAdjacentHTML('beforebegin', '<table border=1><tr><td>');
            document.getElementById("one").insertAdjacentHTML('afterend', '</td></tr></table>');
<body onload="addTable()">
    <div id="one">one</div>

When I called insertAdjacentHTML, why it does not do this?

<table border=1><tr><td>
                <div id="one">one</div>



Link to comment
Share on other sites

When you use insertAdjacentHTML it parses the HTML and creates nodes, and inserts those nodes.  Any unclosed HTML tags will be automatically closed.  So when you do the first line, it will automatically close that td, tr, and table and insert the new table element.  It's not an arbitrary string of HTML text that it inserts, it is entire nodes.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...