Uncaught DOMException: Document.body setter: The new body must be either a body tag or…
- User
Uncaught DOMException: Document.body setter: The new body must be either a body tag or frameset tag.
Assistant
This error message means you're trying to set the
document.bodyproperty to something that's not a valid HTML<body>or<frameset>element.Here's a breakdown of the problem and how to fix it:
- User
const cloneMainPage = wrap.cloneNode(true); document.body.innerHTML = cloneMainPage; how to get hole cloneMainPage with child tags
Assistant
You're on the right track! The issue is that
document.body.innerHTML = cloneMainPage;replaces the entire existing content of the<body>with the cloned content.Here's how to get the complete
cloneMainPagewith all its child tags:1. Append the Clone:
Instead of replacing the entire body, append the cloned content to the existing body:
javascriptconst cloneMainPage = wrap.cloneNode(true); document.body.appendChild(cloneMainPage);This will add the cloned content as a child of the existing
<body>.2. Accessing the Clone:
After appending, you can access the complete
cloneMainPage(including its child tags) using thecloneMainPagevariable itself.- User
why it doesn't work after second running?
function testBtnListener() { console.log('testBtnListener'); arrMarkerTest.forEach((el) => { console.log('el: ', el);
codeel.addEventListener('click', () => { document.body.innerHTML = bodyTestHTML; const nextBtn = document.getElementById('nextBtn'); nextBtn.addEventListener('click', () => { document.body.replaceChildren(cloneMainPage); testBtnListener() }) }) })} testBtnListener();
Assistant
You're encountering this issue because you're repeatedly replacing the entire body content with
document.body.innerHTML = bodyTestHTML;and then trying to replace it again withdocument.body.replaceChildren(cloneMainPage);. This overwrites the previous content and event listeners each time.- User
target.matches('#test1')
how to check the array of [#test1,#test2,#test3]
Assistant
You can use the
Array.includes()method to check if an array contains a specific element.Here's how to check if
targetmatches any of the elements in your array:javascriptconst targetArray = ['#test1', '#test2', '#test3']; if (targetArray.includes(target.id)) { console.log('Target matches an element in the array!'); // Do something } else { console.log('Target does not match any element in the array.'); }Explanation:
targetArray: This array holds the IDs you want to check against.target.id: This gets the ID of the element you're currently working with.Array.includes(): This method returnstrueif the array contains the specified element, andfalseotherwise.
Example:
html