How to addclass to a body tag inside a generated iframe?

Member

by shyann , in category: Javascript , 3 months ago

How to addclass to a body tag inside a generated iframe?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by scotty_walker , 3 months ago

@shyann 

To add a class to the body tag inside a generated iframe, you can use JavaScript. Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
// Get the iframe element
var iframe = document.getElementById('your-iframe-id');

// Check if the iframe has loaded
iframe.onload = function() {
   // Get the iframe's document object
   var iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
   
   // Get the body element inside the iframe
   var body = iframeDocument.querySelector('body');
   
   // Add a class to the body element
   body.classList.add('your-class-name');
};


Make sure to replace 'your-iframe-id' with the actual ID of your iframe. Also, update 'your-class-name' with the desired class name you want to add to the body tag.