0

I am applying style values on div array and it is working fine. I am able to find the div with class in the code below but i have to select section and articles with class as well.

var selectors = ["header-wrap", "top-page", "footer","sidepanels"];
var arrayLength = selectors.length;
for (var i = 0; i < arrayLength; i++) {   
    var parentDoc = window;
    while (parentDoc !== parentDoc.parent) {
        parentDoc = parentDoc.parent;
    }
    parentDoc = parentDoc.document;

    //getting div content
    var divContent = parentDoc.getElementsByClassName(selectors[i])[0];
    if (divContent) {

    divContent.style.position = 'relative';
    divContent.style.zIndex = '1';
    }

The above code is working fine. Is there any better approach in jquery of doing the same thing ?

Buzinas
  • 11,597
  • 2
  • 36
  • 58
Owais Ahmed
  • 1,364
  • 1
  • 30
  • 62

1 Answers1

0

Yes, you can easily do that in jQuery.

Assuming that jQuery is referenced in the root window, you can do:

// you don't need to loop through the parents to get the root window, you can use window.top
window.top.$('.header-wrap', '.top-page', '.footer', '.sidepanels') // pass the classes to $
  .css({ position: 'relative', zIndex: 1 }); // changes the CSS of all the found elements

If jQuery is referenced in the iframe, then you can do:

$(window.top.document) // get the most parent document
  .find('.header-wrap', '.top-page', '.footer', '.sidepanels') // pass the classes to jQuery
  .css({ position: 'relative', zIndex: 1 }); // changes the CSS of all the found elements
Buzinas
  • 11,597
  • 2
  • 36
  • 58