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

Invisible anchor rows in GWT's UIBinder grid view

While developing my GWT application, I encountered an issue in a composite class where I am creating a Grid UI binder. Each cell of the grid contains an anchor UI Binder element. However, upon loading the page in the application, I noticed that the table w ...

What is the best way to place an <a> tag and a <p> tag side by side in an HTML

I have a question on organizing this HTML code: <ol class="results-list"> <% @results.sort_by { rand }.each do |result| %> <li class="<%= 'checked-out' if result.catalog_item.library_status == 'Checked out&apos ...

React components are failing to display data as expected

I need to display certain data based on the id provided in the url. When I use console.log() with res.json, I can see the data but I'm unsure how to pass it to the 'articleComponent'. const Articles = () => { const query = (id) => ...

Implement a loader in AngularJS to display when transitioning between pages

Is there a way to implement a loader that appears when the page starts changing and only disappears once the entire page is fully rendered to prevent clipping bugs? I have already set up the loader as shown below: $scope.$on('$routeChangeStart' ...

The implementation of Ajax beforeSend and complete functions is not possible at the moment

I’ve been attempting to implement a spinner image while loading ajax data, but I can't seem to get it to work. Here's my code : $.ajax({ url: '/Observer/GetInfoProfileByProfileId', type: 'POST', data: { ProfileId: ...

Improprove the efficiency of rendering cubes in threejs

Here is the code snippet I am working with: https://github.com/CrizzzSombiii/laboratoryofsombiisbycrizz/blob/master/docs/maze2.htm <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/88/three.js"></script> <script> // Code f ...

Amending the Access-Control-Allow-Origin setting to enable the proper functioning of Jquery's load() function

SITUATION: Our internal web site is hosted on a web server. We also have SharePoint running on a separate internal web server. Both are within the company.com internal domain, with different sub domains accessed via SharePoint.company.com and internalWeb ...

JSON.Parse function does not return a valid value

I'm currently developing a polling system. Once a user submits their answer choice, I expect to receive JSON data containing all the answers for display purposes. Upon submitting the AJAX form, the JSON response is as follows: [{"answer_1":0,"answer ...

retrieve the value of a textarea element from an HTML table

Is there a way to extract data from a nested table's textarea using the row index? I attempted the following code snippet but it did not work. var note = document.getElementById($index).cells[3]; var y = document.getElementsByTagName("textarea").valu ...

Creating and sending an email using a contact form in Create-React-App

Currently, I am in the process of developing a website using create-react-app from GitHub. My next task is to create a contact page where user information will be submitted and sent to a designated email address. However, my lack of experience with back-e ...

What is the most effective method for nesting loops in NodeJS and Mocha?

Currently, I am attempting to create a loop within a loop in my NodeJS code, but I seem to be getting lost along the way. The results are not as expected - sometimes callbacks are triggered twice and so on. My approach involves using the async module. I wo ...

Interested in creating a Twitter bot that can automatically share images from a designated folder in sequential order. Leveraging the power of node.js

Recently, I've been following an online guide to create a Twitter bot that tweets images from a folder on my computer. While I have a vast collection of photos I'd like to share, the guide only demonstrates how to post them in a random sequence. ...

Can you explain the concept of "cascading" within CSS?

Can someone kindly explain the specific definition of "Cascading" in Cascading Style Sheets (CSS)? I have heard conflicting perspectives on this topic, so I am seeking clarification here. Any examples to illustrate would be greatly appreciated. ...

Despite the presence of data, MongoDB is not returning any values

I am currently working on a code that utilizes $geoNear to find the closest transit stop to a given GPS coordinate. The issue I am facing is that the result sometimes returns undefined, even though I have confirmed that the data exists in the STOPS collect ...

Preventing Data Duplicates When Inserting in ASP.NET MVC

I have noticed that when sending an insert query to my Oracle database through MVC ACTION, the insert method seems to be called twice and records the same data twice... Furthermore, when using the same structure to run a select query, I am getting the sam ...

Web browser displaying vertical scroll bars centered on the page

I'm new to HTML and have been experimenting with this CSS file to center my form. I've managed to get it working, but the issue now is that it's causing scroll bars to appear on the web browser. How can I resolve this without them? @import ...

Can a Chrome App access a user's files on their hard drive with proper permissions?

I'm currently working on a Chrome Packaged App that includes video playback functionality. My main goal is to enable users to stream online media (such as MP4 videos) while also giving them the option to save the video to a location of their choice. ...

Trouble encountered while attempting to choose a single checkbox from within a v-for loop in Vue.js?

<div id="example-1"> <ul> <input type="text" v-model="searchString" placeholder="Filter" /> <p>sortKey = {{sortKey}}</p> <li v-for="item in sortedItems"> <input class="checkbox-align" type="checkbo ...

Every time I switch to a new Vue.js route, I find myself inexplicably redirected to the bottom of the page, leaving me scratching my head in confusion

I'm really struggling to understand why this issue is happening because there doesn't seem to be any code that would interfere with the scrolling. Every time I click on the link, it immediately takes me to the bottom of the view. I'm not sur ...

Ways to dynamically emphasize text within ngFor loop

Within my ngFor loop, I have a set of rows. <div *ngFor="let block of data;"> <div class="class-row"> <div class="left">A Label:</div> <div class="right">{{block.key1}}</div> </div> <div class="clas ...