document.getElementbyID returns NULL for the DIV with in iFrame

854
February 21, 2017, at 2:27 PM

I have a JSP page which loads a static HTML page using iFrame. Now i am trying to change the alignment of one of the DIV (which is there in static HTML page) using the jQuery dynamically. But its not working.

var divEnbref = document.getElementById("enbref");
if(divEnbref!=''){
  $('#enbref').css("text-align" , "left");
}

DIV

<div id="enbref">Some Content</div>

Can someone help me.

Answer 1

use .innerHTML to get the content of your div

var divEnbref = document.getElementById("enbref").innerHTML;

then use condition statement

if(divEnbref!=''){
  $('#enbref').css("text-align" , "left");
}
Answer 2

As you are selecting element inside iframe you need to select the iframe first.

document.getElementById('myframe1').contentWindow.document.getElementById('enbref')

try this..

Answer 3

Try this :

var divEnbref = document.getElementById('enbref');
document.getElementById('enbref').style.textAlign = "right";

I've tried this in fiddle: https://jsfiddle.net/mgs4y3px/

May this will help you!

Rent Charter Buses Company
READ ALSO
css vertical align text middle with overflow hidden

css vertical align text middle with overflow hidden

i'm rendering divs within a loopeach div has a text (could be long- which means overflow: hidden is needed

601
Animate height of div with changing content?

Animate height of div with changing content?

I've got a div with the ID testimonials that has a height of autoIn the div are a number of section's that vary in height

484