Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide an element if another element exist

I have a button with classname ADD to create an i-frame.

when clicked on add I would like to hide the button if that iframe creates. is there a way to hide div with classname : add after iframe creation?

Here is my snippet :

$(document).ready(function() {
  $(".adding").click(function(e) {
    e.preventDefault();
    $('#iframeplace').html('<span class="loading">LOADING...</span>');

    setTimeout(function() {
      $("#iframeplace").html("<iframe name='someFrame' id='someFrame' width='560' height='315'></iframe>");
    }, 3000);
  })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<a href="secondpage.html" class="adding">
  <div class="ADD">ADD</div>
</a>
<div id="iframeplace"></div>
like image 603
inaz Avatar asked Jan 02 '17 09:01

inaz


People also ask

How would you hide the element while still maintaining the element space?

You can hide an element in CSS using the CSS properties display: none or visibility: hidden . display: none removes the entire element from the page and mat affect the layout of the page. visibility: hidden hides the element while keeping the space the same.

How do you completely hide an element?

Completely hiding elements can be done in 3 ways: via the CSS property display , e.g. display: none; via the CSS property visibility , e.g. visibility: hidden; via the HTML5 attribute hidden , e.g. <span hidden>

How do you hide an element in HTML?

To hide an element, set the style display property to “none”. document. getElementById("element").


2 Answers

You can achieve that by using .length which returns the number of elements for the specified selector. In your case the selector would be #iframeplace.

How you will hide the item is completely up to you - you can either use jQuery hide method or hide it using the CSS.

$(document).ready(function() {
  $(".adding").click(function(e) {
    e.preventDefault();
    $('#iframeplace').html('<span class="loading">LOADING...</span>');

    setTimeout(function() {
      $("#iframeplace").html("<iframe name='someFrame' id='someFrame' width='560' height='315'></iframe>");
    }, 3000);
    
    if($('#iframeplace').length == 1)
    {
        $(".adding").hide();
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<a href="secondpage.html" class="adding">
  <div class="ADD">ADD</div>
</a>
<div id="iframeplace"></div>
like image 183
wegelagerer Avatar answered Oct 23 '22 14:10

wegelagerer


It's hard to say which style a and nested div have, so you can hide either a with class name adding or div with class name ADD. Anyway you need to do it after iframe load.

$(document).ready(function() {
  $(".adding").click(function(e) {
    e.preventDefault();
    $('#iframeplace').html('<span class="loading">LOADING...</span>');

    setTimeout(function() {
      $("#iframeplace").html("<iframe name='someFrame' id='someFrame' width='560' height='315'></iframe>");
      $(".ADD").css("display", "none");
   }, 3000);
  })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<a href="secondpage.html" class="adding">
  <div class="ADD">ADD</div>
</a>
<div id="iframeplace"></div>
like image 40
Banzay Avatar answered Oct 23 '22 15:10

Banzay