The Wayback Machine - https://web.archive.org/web/20240718030026/https://www.geeksforgeeks.org/dynamically-create-and-remove-iframe-in-javascript/
Open In App

Dynamically Create and Remove Iframe in JavaScript

Last Updated : 04 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

JavaScript allows us to dynamically add and remove elements to the document. We can use event listeners and special JavaScript methods to create and remove HTML elements. This article focuses on creating and removing iframe elements dynamically using JavaScript.

Let’s first create a basic structure of the document using buttons and other elements. Buttons will allow us to call the JavaScript methods.

Flow Diagram Image for Dynamic Iframe:

Steps to Dynamically Create and Remove Iframe in JavaScript

  • First, Create an HTML Structure for the page containing title, 2 buttons (create and remove iframe) and dummy iframe using the html elements like div’s, span, iframe, buttons etc along with class name and some inline styles.
  • Define a function createIframeElement that creates and render the iframe dynamically on the web-page and assign with create iframe onclick event.
  • The createIframeElement uses createElement() to create iframe and its define properties like srcdoc, height and width using JavaScript.
  • Define a function removeIframeElement that calls when removeiframe button is clicked and removes the dynamically created iframe from the page with help of removeChild() method.

Example: This example demonstrates the code to dynamically create and remove iframe.

HTML




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0">
</head>
 
<body>
    <h2 style="color:green;">
        GeeksforGeeks
    </h2>   
    <h3>Dynamically create and remove iframes</h3>
    <div id="display"></div>
    <button onclick="CreateIframeElement()">
        CreateIframe
    </button>
    <button onclick="RemoveIframeElement()">
        RemoveIframe
    </button>
    <script>
        const CreateIframeElement = () => {
            // Creating iframe element.
            let el = document.createElement("iframe");
 
            // setting the values for the attributes.
            el.srcdoc = `<h1>.Iframe Element.</h1>
            <p> Hello Geek! <br> How are you? </p>`;
            el.width = "400px";
            el.height = "200px";
 
            // Adding the created iframe to div as a child element
            document.getElementById("display").appendChild(el);
        }
 
        const RemoveIframeElement = () => {
 
            // Remove the last child ( iframe element ) of div.
            document.getElementById("display")
                .removeChild(document
                .getElementById("display").lastChild);
        }
    </script>
</body>
 
</html>


Output:

Dynamically create and remove iframe



Similar Reads

How to check if an iframe is present in another iframe in JavaScript ?
In this article, we will see how to check if an iframe is present in another iframe, along with understanding its basic implementation with the help of a simple example. An iFrame is an HTML element that allows users to embed a webpage within another webpage. It's frequently used for displaying content from another website, such as a video or a soc
4 min read
How to load a hyperlink from one iframe to another iframe ?
HTML Iframe or inline frame allows embedment of multiple HTML pages in an HTML page. In this article, we will see how to change the content of one iframe using hyperlinks in another iframe. This can be achieved by the proper usage of the name attribute in the iframe tag and the target attribute in the anchor tag. The name attribute allows each inli
2 min read
Remove border from IFrame using CSS
The Iframe is used to display a web page on the webpage. Remove border from iframe tag in the webpage could be done by using one of the CSS properties of the iframe tag called frameBorder and set its value to "0". Syntax: frameBorder = "value"; Note: In the frameBorder property the B letter must be in capital otherwise it will not be recognized by
1 min read
How to dynamically create and apply CSS class in JavaScript ?
To dynamically create a CSS class and apply it to the element dynamically using JavaScript, first, we create a class and assign it to HTML elements on which we want to apply CSS property. We can use className and classList properties in JavaScript. ApproachThe className property is used to add a class in JavaScript. It overwrites existing classes o
2 min read
How to add and remove input fields dynamically using jQuery with Bootstrap ?
In this article, we will learn how to add and remove input fields dynamically using jQuery with Bootstrap. The input fields will be added and removed from the HTML document dynamically using jQuery. In this process, we will use the following multiple functions. Click() Method: Used to attach the click event to the buttonAppend() Method: Used to app
3 min read
How to Create a Form Dynamically with the JavaScript?
The task is to create an HTML form dynamically with the help of JavaScript. There are two approaches that are discussed below.Approach 1: Use document.createElement() to create the new elements and use setAttribute() method to set the attributes of elements. Append these elements to the &lt;form&gt; element by appendChild() method. Finally append t
3 min read
How to dynamically create new elements in JavaScript ?
New elements can be dynamically created in JavaScript with the help of createElement() method. The attributes of the created element can be set using the setAttribute() method. The examples given below would demonstrate this approach. Example 1: In this example, a newly created element is added as a child to the parent element. The type of the elem
3 min read
How to create XML Dynamically using JavaScript?
XML stands for Extensible Markup Language. It is a popular format for storing and exchanging data on the web. It provides a structured way to represent data that is both human-readable and machine-readable. There are various approaches to create XML Dynamically using JavaScript: Table of Content Using DOM ManipulationUsing Template LiteralsUsing DO
2 min read
How to create an image element dynamically using JavaScript ?
Let's explore how to dynamically create an &lt;img&gt; element in HTML using JavaScript. When a button is clicked, we'll generate the &lt;img&gt; element and append it to the document. These are the following ways to create an image element dynamically: Table of Content Using createElement() methodUsing image() constructorApproach 1: Using createEl
2 min read
How to Create a Transparent iframe?
The &lt;iframe&gt; tag is an inline frame. It is used to embed another HTML page within the current HTML page. Syntax&lt;iframe src = "URL"&gt;&lt;/iframe&gt;ApproachIn this approach we will use iframe tag to create the iframe and A transparent iframe can be made by setting its background to transparent. And, the allowtransparency attribute of “ifr
1 min read
three90RightbarBannerImg