Tips for refreshing a division in an HTML editor

Is there anyone who can assist me with updating a div within an htmleditor? The div is located inside an iframe, so I am unable to use the following code:

Ext.fly('mydiv').update('New value');

Currently, I am refreshing the entire content using:

xtype:'textfield',
listeners:{
                keyup:{
                    fn:function(){
                        var e = this.up().down('htmleditor');
                        var v = 'some text <div id='mydiv'></div> some more text'
                        e.setValue(v);
                    }
                }
            },
xtype:'htmleditor',
...

However, I would like to only update the mydiv section without being able to reference it. Any help on this issue would be greatly appreciated. Thank you.

Answer №1

It seems the main question at hand is, "How can I retrieve a DIV element within an IFRAME from its parent document?"

I'm unfamiliar with EXT JS syntax, but here's a simple JavaScript approach.

First, let's look at the document inside the IFRAME:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Child Document</title>
</head>
<body>

<h1>Child Document</h1>

<div id="stuff">
This div contains some text.
</div>

</body>
</html>

Now, let's move on to the parent document:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Parent Document</title>
    <style type="text/css">
     #iframe {
        width: 250px;
        height: 250px;
        border: 1px solid #CCC;
    }
    </style>
</head>
<body>

<h1>Parent Docment</h1>

<form action="" method="get">
    <p>
    <input type="button" id="get-it" value="Get It" />
    </p>
</form>

<iframe src="iframe.html" id="iframe"> </iframe>

<script type="text/javascript">
<!--
function getIt(){
    var iframe = document.getElementById("iframe");
    var innerDoc = iframe.contentDocument || iframe.contentWindow.document;
    var stuff = innerDoc.getElementById("stuff");
    alert(stuff.innerHTML);
}

function init(){
    document.getElementById("get-it").onclick = getIt;
}

window.onload = init;

// -->
</script>

</body>
</html>

The crucial line is this:

var innerDoc = iframe.contentDocument || iframe.contentWindow.document;

This allows you to access the IFRAME's document object and manipulate it as needed.

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

Successive jquery functions are executed independently upon being called

I've encountered a simple problem that has stumped me despite trying various approaches to consolidate these functions in one HTML file. The issue arises when the first function is invoked, causing the second function to execute simultaneously and lea ...

Is there a way to avoid waiting for both observables to arrive and utilize the data from just one observable within the switchmap function?

The code snippet provided below aims to immediately render the student list without waiting for the second observable. However, once the second observable is received, it should verify that the student is not enrolled in all courses before enabling the but ...

Should Angular libraries be developed using Typescript or transpiled into JavaScript?

Currently, I am in the process of developing a library that will be available as an Angular module through npm. The library itself has been written using typescript. Everything was functioning perfectly up until Angular version 5.0.0, but after this update ...

Modifying the background color of a div with ng-click functionality

Having trouble changing the background color of a div that is already styled with white for odd elements and grey for even elements. I'm not sure why it's not working in Jsfiddle. The div also has an ng-click attribute that I haven't include ...

Alert: The `previousStep` prop on a DOM element is not recognized by React

import React, { Component } from 'react'; import ChatBot from 'react-simple-chatbot'; import { Switch, Route, BrowserRouter } from 'react-router-dom'; import Dropzone from 'react-dropzone'; class Form extends C ...

Reversing the order of elements when using the after() method in a jQuery loop

I am facing an issue with an array containing HTML tags as strings. Here is the array: array = [ "<p>1</p>", "<p>2</p>", "<p>3</p>" ] I have a for loop that utilizes the after() method : fo ...

Connecting a navigation bar on various pages without using server side scripting

Looking to create a linked navbar that spans multiple pages on my website without relying on server-side scripting like PHP. I've attempted to use the jQuery load function at the footer of each page to load the navbar into the #navbar element. While i ...

Creating a new URL using a JSON response in Node.js: A step-by-step guide

I am facing a query regarding the following code: The issue at hand is: How can I pass each line from response promiseGetCitiesData to promiseGetInformationDataPerCity. Is it possible to achieve this in a single async.each function? Currently, I have ...

how to set a boolean value to true in a vue @click event?

@click.native="scrollTo(index,true)" My expectation: Always pass Boolean:true into the scrollTo function. Vue's reaction: Vue interprets true as a variable name, resulting in Number:index and undefined instead. Solution: ...

Utilize the same variable within a React functional component across multiple components

Within a React functional component, I am utilizing the following constant: const superHeroPowers = [ { name: 'Superman', status: superManPowerList }, { name: 'Batman', status: batmanPowerList }, { name: 'Wonder Woman&a ...

How come it functions with $scope but fails with `this`?

I am trying to figure out why my code only works with scripts 1 and 3, but not with script 2. I need this functionality for my project because I can't use $scope. Thank you!! <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.19 ...

Injecting modules dynamically in AngularJS

Is it possible to dynamically perform dependency injections? For example, injecting modules later rather than when creating the root app? I have a website where I want to create one all-encompassing ng-app instead of having separate ng-apps for different ...

Oops! Looks like we have encountered an issue with reading the property 'checked' of nothing

Whenever I click the checkbox, an image should be displayed. Unfortunately, I encountered an error: Uncaught TypeError: Cannot read property 'checked' of null at (index):185 at dispatch (VM576 jquery.min.js:3) at i (VM5 ...

Engaging SVG Experience: Alter the color of one element by hovering over another

I'm currently working on creating an interactive SVG map. I have successfully incorporated code from Illustrator into an HTML file and managed to change the fill color of certain paths when hovering over them. However, I am facing an issue where a whi ...

`Need help streamlining your async/await calls into one simple Promise.all statement?``

The current try/catch block is making 3 API calls, but it takes a long time to execute when the dataset in firstData is large. try { const firstData = await myservice1.myservice1Func(); for(let i=0; i<firstData.total; i++){ const hostNa ...

Retrieving precise information from the backend database by simply clicking a button

As a new full stack programmer, I find myself in a challenging situation. The root of my problem lies in the backend table where data is stored and retrieved in JSON format as an array of objects. My task is to display specific data on my HTML page when a ...

Transform Vue military time to regular time format

I am retrieving the arrivalTime from todos.items. As I fetch the arrivalTime, it is sorted using the sortedArray function(), which converts the times to military time. Is there a way to change the sorted military time values to standard time format after s ...

What is the process for including icons in Tamagui Input?

I am currently utilizing the Tamagui Input component and I am interested in incorporating icons to enhance its visual appeal. To align buttons with icons alongside my Input component, I have resorted to using a combination of Stacks similar to the followi ...

checking the Arduino digital pins' status on a web page

Looking for a way to read and display the statuses of digital pins from an Arduino on a web page using Flask for web programming. The code attempted so far is not functioning as expected. On the Arduino side, values of 6 digital pins are being read in the ...

Using object functions within middleware functions

Within my middleware, I have the following code snippet: const UserMiddleware = { isNumber(n) { return !Number.isNaN(parseFloat(n)) && !Number.isNaN(n - 0); }, // eslint-disable-next-line consistent-return validateSignUp(req, res, next) { ...