Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get iframe inside div with javascript

Tags:

javascript

dom

Is there a way to do the following in JavaScript?

  1. get a div by a class name,
  2. get the iframe inside the div
  3. and add an attribute (frameborder = 0)

<html>
    <body>
        <div class="calendar">
            <iframe></iframe>
        </div>
    </body>
</html>
like image 908
Rod Avatar asked Oct 30 '25 21:10

Rod


2 Answers

To get a div by class name:

var allDivs = document.getElementsByClassName("calendar");

Assuming there's only one

var calendarDiv = document.getElementsByClassName("calendar")[0];

Get the iFrame inside:

var iFrame = calendarDiv.getElementsByTagName("iframe")[0];

set an attribute:

iFrame.setAttribute("frameborder", "0"); 

Or, since getElementsByClassName isn't supported in old versions of IE, consider setting the id on the iFrame and simply doing:

document.getElementById("iFrameId").setAttribute("frameborder", "0"); 
like image 168
Adam Rackis Avatar answered Nov 02 '25 12:11

Adam Rackis


First of all, you need to give your iframe id.

After that, you can get your iframe with the function document.getElementById(""), and than to change it's property.

For example:

<html>
<body>
    <div class="calendar">
        <iframe id="myIframe"></iframe>
    </div>
    <script type="text/javascript">
        document.getElementById("myIframe").frameborder = 1;
    </script>
</body>
</html>
like image 29
matan7890 Avatar answered Nov 02 '25 11:11

matan7890