Although two iframes have the same class, this will hide only the first iframe.
function hide() {
var iframe = document.getElementsByClassName("iframe");
iframe[0].style.display = "none";
}
<iframe class="iframe"></iframe>
<iframe class="iframe"></iframe>
<br>
<button onclick="hide()">Hide 1st Iframe</button>
What does this code does?
document.getElementsByClassName("iframe")
gets all the elements with the class iframe
and set the elements as the value of a variable named iframe
. The number inside []
defines one element of the many. If the number inside []
is 0
, the first element out of the group of elements is defined. After []
, you can set the property you want.