Find a specific tag in a div that has no ID
I am using jquery and am trying to find a tag <html>
embedded in <object>
. I am trying to use find method but cannot find it. Can anyone ask for help.
code:
if (dialogObject.find('head')) {
var findelemnt = $('#dialogObject').find('html');
var testthis = findelemnt.attr("class");
alert(testthis);
}
else { alert("not found") }
dialoObject is <div>
. Screen pinning on the page.
source to share
I think this probably fails due to the same origin policy . See. Fooobar.com/questions/2154425 / ... .
Try it with a local url - I guess you can get it to work there using this code:
$('#dialogObject')[0].contentDocument
source to share
If you are trying to inject a nested browsing context, you can add a "name" attribute:
<object id="dialogObject" data="http://domain/path" name="myWindow"></object>
and access the nested element <html>
with myWindow.document.documentElement
or window["myWindow"].document.documentElement
.
EDIT 1: Although you have access to the element <object>
using its "id" attribute for example. window.dialogObject
, window["dialogObject"]
or of course document.getElementById("dialogObject")
you can access the nested object window
using its "name" attribute, for example. myWindow
or window["myWindow"]
.
It is $(myWindow.document.documentElement)
equivalent to using $(myWindow.document).find('html')
.
EDIT 2: If you prefer to use the "id" attribute as per your original code, you can combine Matt Browne's answer with the above to achieve the following:
$($('#dialogObject')[0].contentDocument).find('html')
although I would suggest the following instead:
$(document.getElementById("dialogObject").contentDocument.documentElement)
Also, don't forget to run your code in your document event handler to make sure the DOM is fully loaded by the time you try to access any element in question.
source to share