Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getElementById from iframe

Tags:

javascript

could someone help me to understand why this errors

document.getElementById("actContentToGet").contentWindow.document.body.getElementById is not a function

function deleteElement(element){
        var elementID = $(element).attr("class");
        alert(elementID);
        document.getElementById('actContentToGet').contentWindow.document.body.getElementById(elementID).remove;
        alterContent();
        giveAllIDs();
        hoverLoad();
    }
like image 956
Phil Jackson Avatar asked Dec 13 '09 12:12

Phil Jackson


People also ask

Can I get element from iframe?

contents() can get both text nodes and HTML elements. That's why one can get document contents of an iframe by using it. Note that the iframe and page have to be on the same domain.

Can you use JavaScript in an iframe?

It is called an inline frame because to the user it is all one web page. The child iframe is a complete browsing environment within the parent frame. It can load its own JavaScript and CSS separate from the parent.

How do I access iframe?

To get the element in an iframe, first we need access the <iframe> element inside the JavaScript using the document. getElementById() method by passing iframe id as an argument. const iframe = document.

How do I read Cross domain iframe content?

You can read the contents in an iframe easily via jQuery ( $(iframe). contents() ) if the domain of the webpage which contains the iframe is same as the domain of the web-page opened in iframe . The microservice responsible for uploading file was on different domain and the main app is on another domain.


2 Answers

Try changing this:

...contentWindow.document.body.getElementById(elementID)...

to this:

...contentWindow.document.getElementById(elementID)...

Edit from comments: It's not removing that element because that's not how you remove elements. Try this:

var iframe = document.getElementById('actContentToGet');
var frameDoc = iframe.contentDocument || iframe.contentWindow.document;
var el = frameDoc.getElementById(elementID);
el.parentNode.removeChild(el);

See the documentation here.

like image 64
nickf Avatar answered Sep 18 '22 12:09

nickf


Try removing the body.- getElementById() is a document. function.

like image 42
Pekka Avatar answered Sep 17 '22 12:09

Pekka