A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

How to insert HTML content into an iFrame using jQuery
Q:

How to insert HTML content into an iFrame using jQuery

0

How to insert HTML content into an iFrame using jQuery

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Use the jQuery contents() method

You can use the jQuery contents() method in combination with the find()val() and html() methods to insert text or HTML inside an iframe body.

Let's try out the following example to understand how it basically works:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Insert HTML inside an iFrame</title>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<style>
    textarea, iframe{
        display: block;
        margin: 10px 0;
    }
</style>
<script>
    function updateIframe(){
        var myFrame = $("#myframe").contents().find('body');
        var textareaValue = $("textarea").val();
        myFrame.html(textareaValue);
    }
</script>
</head>
<body>
    <textarea rows="5" cols="50" placeholder="Type HTML or text here..."></textarea>
    <button type="button" onclick="updateIframe()">Insert Content</button>
    <iframe id="myframe"></iframe>
</body>
</html>

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now