How to remove any div in iframe?

Member

by gilbert , in category: Javascript , 2 months ago

How to remove any div in iframe?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by ryan.murray , 2 months ago

@gilbert 

To remove a div inside an iframe, you can use JavaScript to target the specific div element and remove it from the DOM.


Here is an example code snippet to remove a div with id "myDiv" from an iframe with id "myFrame":

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// Get the iframe element
var iframe = document.getElementById("myFrame");

// Get the document object of the iframe
var iframeDoc = iframe.contentDocument || iframe.contentWindow.document;

// Get the div element inside the iframe
var divToRemove = iframeDoc.getElementById("myDiv");

// Remove the div element from the iframe
divToRemove.parentNode.removeChild(divToRemove);


Make sure to replace "myFrame" and "myDiv" with the actual id of your iframe and div elements. Also, keep in mind that you may need to handle any cross-origin security restrictions when accessing the content of the iframe using JavaScript.