joecool2005 Posted June 5, 2018 Share Posted June 5, 2018 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 More sharing options...
justsomeguy Posted June 5, 2018 Share Posted June 5, 2018 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now