Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close a parent p tag after closing the iframe?

I have an iframe, which comes up as a pop up . Just above it is a p tag consisting of close button.

Now, on clicking one of the links on the frame, i am closing it like this :

 window.parent.document.getElementById('myIframeId').
 parentNode.removeChild(window.parent.document.getElementById('myIframeId'));

However, the p tag still exists and does not close up . This is the code :

<p class="close"><a href="#"><img src="buttonClose.jpeg" alt="Close"/></a></p>
<iframe id="myIframeId" style="display:none;" height="430" width="675" src="myjsp.jsp" ></iframe>

When i see in firebug, i can see that the iframe tag is no more, but the p tag still exists and it does not close , if not closed manually by clicking on the close icon.

EDIT : After the suggestion by David, I have wrapped bot tags inside a div :

<div id = "closeModal">
<p class="close"><a href="#"><img src="buttonClose.jpeg" alt="Close"/></a></p>
<iframe id="myIframeId" style="display:none;" height="430" width="675" src="myjsp.jsp" ></iframe>
</div>

However, everytime now i close the iframe using david's suggestion, anither dynamic div gets added like this :

<div id="myContainerModal" class="" style="z-index: 9999; width: 675px; position: absolute; top: 75px; left: 290px; opacity: 0.999999;">
<div id = "closeModal">
<p class="close"><a href="#"><img src="buttonClose.jpeg" alt="Close"/></a></p>
<iframe id="myIframeId" style="display:none;" height="430" width="675" src="myjsp.jsp" ></iframe>
</div>
</div>

Now how can i get rid of the dynamic div : myContainerModal , it is not going out of the screen ?

How can i close this p tag after closing the iframe? Kindly help .

like image 458
The Dark Knight Avatar asked Feb 18 '23 10:02

The Dark Knight


2 Answers

You could access #myIframeId's previousElementSibling, which would be the p.

var iframe = window.parent.document.getElementById('myIframeId');
iframe.parentNode.removeChild(iframe.previousElementSibling);
iframe.parentNode.removeChild(iframe);

But it would've been simlper, and arguably neater, to wrap the two elements in a div and remove that by ID, rather than the iframe.

HTML:

<div id="iframe-container">
   <p class="close">...</p>
   <iframe>...</iframe>
</div>

JS:

var iframe = window.parent.document.getElementById('iframe-container');
iframe.parentNode.removeChild(iframe);
like image 178
David Hedlund Avatar answered Feb 21 '23 02:02

David Hedlund


You can use this:

var p = document.getElementsByClassName("close")[0]
p.parentNode.removeChild(p);

It would be better to add a id="someName" to the p, though, like this:

<p class="close" id="close"><a href="#"><img src="buttonClose.jpeg" alt="Close"/></a></p>
<iframe id="myIframeId" style="display:none;" height="430" width="675" src="myjsp.jsp" ></iframe>​

Then you can delete it as follows:

var p = document.getElementById("close")
p.parentNode.removeChild(p);

This will prevent you from having to hardcode the index ([0]) into your js.

like image 40
Cerbrus Avatar answered Feb 21 '23 01:02

Cerbrus