Reputation: 3914
I've scoured the interwebs, and found quite a bit of answers that relate to what I want to do, such as:
Javascript - Get element from within an iFrame
How can I access iframe elements with Javascript?
Access iframe elements in JavaScript
With no proper results.
I currently have a test box in an iFrame. From the Parent, I need to get the value of the textbox onBlur()
.
I'm currently down to using;
alert("before");
var iframe = document.getElementById('GeneralInformationPrivate.aspx');
var innerDoc = iframe.contentDocument || iframe.contentWindow.document;
//innerDoc.getElementById("imageLoc").hide();
alert("after");
With the line commented, I see both alerts.. Without it commented, it doesnt get that far.
(I am hiding it just as a test - Which just doesnt work).
Can anyone point me in the right direction.
Edit The loaded iFrame HTML looks like this:
<iframe id="GeneralInformationPrivate.aspx" class="iframetab" src="GeneralInformationPrivate.aspx"><!-- Controls Here --></iframe>
Edit 2 The iFrames are loaded like so. (In the parent)
<div style="float: right; width: 570px;">
<div id="tabs">
<ul style="">
<li><a class="tabref" href="#tabs-1" rel="GeneralInformationPrivate.aspx">General</a></li>
<li><a class="tabref" href="#tabs-2" rel="www.google.co.za">Friends</a></li>
<li><a class="tabref" href="#tabs-3" rel="">Challenges</a></li>
</ul>
<div id="tabs-1">
</div>
<div id="tabs-2">
</div>
<div id="tabs-3">
</div>
</div>
</div>
On document Load (still in the parent)
// ++ IFRAME TABS
var $tabs = $('#tabs').tabs(); //Set the tab
//Loads the initial tab
function getSelectedTabIndex() {
return $tabs.tabs('option', 'selected');
}
beginTab = $("#tabs ul li:eq(" + getSelectedTabIndex() + ")").find("a");
//Send the correct data to load
loadTabFrame($(beginTab).attr("href"), $(beginTab).attr("rel"));
// Does something everytime you click the tab
$("a.tabref").click(function () {
//event code
});
//Load the actual iFrame
function loadTabFrame(tab, url) {
if ($(tab).find("iframe").length == 0) {
var html = [];
html.push('<div class="tabIframeWrapper">');
html.push('<iframe id="'+ url +'" class="iframetab" src="' + url + '">Load Failed?</iframe>');
html.push('</div>');
$(tab).append(html.join(""));
//$(tab).find("iframe").height($(window).height() - 80); // Set the height of the iFrame (Currently using CSS - See controlStyle.css iFrame)
}
return false;
}
//Make sure its only loaded on click
$("a.tabref").click(function () {
loadTabFrame($(this).attr("href"), $(this).attr("rel"));
});
// -- IFRAME TABS
Which thel loads the page into the iFrame.
Edit 3 Contents of iFrame (sorry)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GeneralInformationPrivate.cs" Inherits="member.Default2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>General Information</title>
</head>
<body style="background: white">
<form id="form1" runat="server">
<div style="min-height: 100px; width: 100%; padding: 0px" id="divProInfo">
<table style="width: 100%">
<tr>
<td>
<div class="divRegInput" style="padding: 5px 0px 10px 15px">
<asp:Label ID="Label12" runat="server" Text="Image URL" Font-Bold="True"></asp:Label>
<asp:Label ID="Label13" runat="server" Text="make yourself look good" CssClass="styleLabelWatermarkWashout"></asp:Label>
<br />
<input id="imageLoc" class="styleTextBoxCenter Larger" />
</div>
</td>
</tr>
<tr>
<td>
<div class="divRegInput" style="padding: 5px 0px 10px 15px">
<asp:Label ID="Label6" runat="server" Text="Website URL" Font-Bold="True"></asp:Label>
<asp:Label ID="Label7" runat="server" Text="show others where you came from" CssClass="styleLabelWatermarkWashout"></asp:Label>
<br />
<asp:TextBox ID="TextBox1" runat="server" CssClass="styleTextBoxCenter Larger" />
<asp:Label ID="lblWebsiteTitle" runat="server" CssClass="styleLabelWatermark"></asp:Label>
</div>
</td>
</tr>
<%--<tr>
<td>
<asp:Button ID="Button1" runat="server" Text="Button" CssClass="styleButtonGreenHolder" Height="43px" CausesValidation="False" OnClick="btnChangeChallenge_Click" Width="195px" />
</td>
</tr>--%>
</table>
</div>
</div>
</form>
</body>
</html>
Upvotes: 2
Views: 3112
Reputation: 3914
I have finally stumbled accross the solution.
I had to put the code in: $(window).load(function () {/*code*/})
, after all my pages code.
Only then did it work.
Upvotes: 0
Reputation: 1074028
The line
innerDoc.getElementById("imageLoc").hide();
...is failing because DOM elements don't have a function called hide
on them. That's a jQuery thing.
You can wrap the element in a jQuery instance:
$(innerDoc.getElementById("imageLoc")).hide();
Upvotes: 1