-1

How can I replace the name of element "The_Iframe_Element_Name" with the variable "elemName" in the following:

  function fnFrame(elemName) {
      setTimeout(function () {
          var iframeBody = window.The_Iframe_Element_Name.document.getElementsByTagName("body")[0];
              $(iframeBody).addClass("xyz");
      }, 500);
  }
hsobhy
  • 1,493
  • 2
  • 21
  • 35

2 Answers2

1
function fnFrame(elemName) {
    setTimeout(function () {
        var iframeBody = window[elemName].document.getElementsByTagName("body")[0];
            $(iframeBody).addClass("xyz");
    }, 500);
}

fnFrame('The_Iframe_Element_Name');
Mulan
  • 129,518
  • 31
  • 228
  • 259
1

Use bracket notation:

var iframeName = 'foo';
var iframeBody = window[iframeName].document.getElementsByTagName("body")[0];

Note that you will only be able to access the content of an iframe if it's within the same URL schema as the parent window.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339