The div inside an iframe is not displaying the desired background color as intended from the parent page

The code snippet below is included in the <head> section of the main page:

<script type="text/javascript">
    $(document).ready(function () {
        $('#innerframe').load(function () {
            $(this).contents().find($(".TitleBG")).css("background-color", "red");
        });
    });
</script>

<iframe src="/HomePage.aspx" onload="GetAlerts();" id="innerframe"></iframe>

Following that, there is a sample div on the .aspx page within the iframe:

<div class="col-md-12">
    <div class="col-md-1 TitleBG">Drive</div>
    <div class="col-md-3 TitleBG">Name</div>
    <div class="col-md-1 TitleBG">Type</div>
    <div class="col-md-1 TitleBG">Format</div>
    <div class="col-md-2 TitleBG">Free Space</div>
    <div class="col-md-2 TitleBG">Available Space</div>
    <div class="col-md-2 TitleBG">Drive Size</div>
</div>

I am attempting to change the background-color property of the divs with the 'TitleBG' class using the provided jQuery code. However, it does not seem to be functioning as expected.

The code works when I apply the

$(".TitleBG").css("background-color", "red");
to elements on the parent page.

Answer №1

Try this approach:

use the selector directly as shown below
find(".TitleBG")

Answer №2

It appears that the code mentioned is now deprecated:

$('#innerframe').load(function () {

One must now use:

$('#innerframe').on('load', function () {

Since after version 1.8, the original code in question is no longer valid.

Additionally:

Instead of using

find($(".TitleBG")), you should use

find(".TitleBG")

User Wiizl has pointed out another issue.

Answer №3

<script type="text/javascript">
    $(document).ready(function () {
        $('#innerframe').load(function () {
            var iFrameDOM = $("iframe#innerframe").contents();

            iFrameDOM.find(".TitleBG").css("background-color", "red");
        });
    });
</script

Unfortunately, this code will not function with third-party iframes.

Answer №4

Modifying an iframe page is not possible as it would be too straightforward.

Instead, utilizing AJAX calls and generating DOM elements is a better approach: https://api.jquery.com/jquery.ajax/

Answer №5

Instead of using this, try utilizing the real id and update the search to .find(".TitleBG")

<script type="text/javascript">
    $(document).ready(function () {
        $('#innerframe').load(function () {
            $('#innerframe').contents().find(".TitleBG").css("background-color", "red");
        });
    });
</script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Transforming an AJAX call into a reusable function

My goal is to simplify my ajax calls by creating a function that can be reused. Although I'm unsure if I'm doing it correctly, I would like to attempt this approach. <script> $(document).ready(function(){ var reg_no=$("#reg_no").va ...

How can I personalize the HTML code of images added to my WordPress articles?

I've been on an extensive search trying to find a solution for this issue. I'm aiming to modify the markup of an uploaded image in WordPress from its current form: <div id="attachment_906" style="width: 590px" class="wp-caption aligncenter"&g ...

Efficiently transferring components of a JavaScript project between files

For the first time, I am creating an npm package using ES6 and Babel. However, I am facing difficulties in connecting everything together so that it can be imported correctly by the end user. The structure of my build (output) folder is identical to src: ...

Exploring new classes with jQuery's .not() and :not()?

I am working on a memory game where I need to flip cards and check if two are equal. My issue is that I do not want the function to run when clicking on a card that is already flipped, or on another flipped card. I tried using jQuery's .not() and :no ...

What are the steps to keep a web application from closing on Android when the back button is pressed?

I am currently working on a HTML5 web application and packaging it with Cordova (phonegap) 1.7. My goal is to customize the behavior of the Android back button so that instead of closing the application by default, it navigates back using window.history.b ...

Steps for implementing a single proxy in JavaScript AJAX with SOAP, mirroring the functionality of the WCF Test Client

I am working with a WCF web Service and a javascript client that connects to this service via AJAX using SOAP 1.2. My goal is to pass a parameter to instruct the AJAX SOAP call to use only one proxy, similar to how it is done in the WCF Test Client by unch ...

Ways to retrieve textStatus beyond ajax boundaries

Do you know how to access the textStatus variable after an ajax call? I need to use this variable in an if condition. Can anyone provide assistance? $this->registerJs("colorArray = ['#ff4c4c','#32CD32']; $('.grooveTable') ...

Is there a way to refresh a webpage without the need to reload it

Imagine a scenario where a tab on a website triggers the loading of a specific part of the webpage placed within a div. For example, clicking on a tab may trigger the loading of a file named "hive/index.php". Now, if a user selects an option from an auto ...

Is it possible for jQuery UI Dialog to function similar to an iFrame?

Is there a way to set up my dialog box with a form so that when the form is submitted, only the dialog box changes instead of the browser location? ...

Tips for making Google search results include query strings in the returned links

I need help figuring out how to make Google search results show a URL containing a query string. Here's an example from the project I am currently working on: Instead of this link, Google search returns: If anyone has any suggestions for fixing this ...

Utilizing a search bar with the option to narrow down results by category

I want to develop a search page where users can enter a keyword and get a list of results, along with the option to filter by category if necessary. I have managed to make both the input field and radio buttons work separately, but not together. So, when s ...

Tips for utilizing various broadcast options to trigger Angular controllers according to the user's chosen selection

I'm currently developing an angularjs application that includes multiple date range pickers on a single web page. When a user selects a date range from one of these pickers, I need to send the selected dates to the corresponding angular controller ass ...

Animate the centering of a DIV

Currently, I am working on an animation featuring a series of DIV's contained within a div with the identifier id="wrapper", all styled using CSS3. However, when I hover over the rounded box, the animation is not properly aligned in the center. You ca ...

Module 'BrowserFetcher.js' could not be located

After updating all my npm packages, I encountered an issue when trying to run on my local server. The error message reads: Error: Cannot find module './BrowserFetcher.js' This error specifically points to a line in my puppeteer file located at - ...

What is the best way to choose all text within a code box without highlighting the entire webpage?

I developed a CSS code specifically for my blogspot site to organize and store all of my code and technical snippets. .code { background:#dae6ef; background-repeat:no-repeat; border: solid #5C7B90; border-width: 1px 1px 1px 20px; color ...

Complete the form to redirect to a new webpage

I'm facing an issue here, wondering how I can submit a form on one URL and another form on a different URL simultaneously... For example: form1.php: <form> <input type="text" name="name" id="name"> <input type="submit" href ...

Typescript versus ES5: A comparison of Node.js server-side applications written in different languages

Note: When I mention regular JavaScript, I am referring to the ES5 version of JS. As I lay down the groundwork for a new project, my chosen tech stack consists of Node.js for the back-end with Angular2 for the front-end/client-side, and Gulp as the build ...

Steps for adding a view to an element

Can someone help me with the syntax needed to append my template within the div "#draggableContainers"? I feel like I'm overlooking something simple. <html xmlns="http://www.w3.org/1999/xhtml"> New Page Title <script src="https://ajax ...

After applying the rotateY function, the z-index appears to be malfunctioning

My current project involves creating a page flip effect, which requires two overlapping div elements. Initially, both divs are side by side with the second div having a -webkit-translateY(180deg) property applied to it. The first div has a z-index of 2, wh ...

saving a JSON element in a JavaScript array

Currently, I am utilizing AJAX to fetch data from a database using PHP and then converting it into JSON format. <?php echo json_encode($data); ?> This is the AJAX function being used: ajaxCall("getdata.php", container, function (data) { var co ...