-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,453
  • 2
  • 19
  • 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
  • 119,326
  • 28
  • 214
  • 246
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
  • 319,460
  • 37
  • 290
  • 318