Jump to content

insertAdjacentHTML


joecool2005
 Share

Recommended Posts

Hi

I have this simple code

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

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

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

Thanks,

 

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
 Share

×
×
  • Create New...