jQuery find() get body element from iframe content

Description

jQuery find() get body element from iframe content

View in separate window

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Inserting Textarea Value into an iFrame</title>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<style>
    textarea, iframe{
        display: block;
    margin: 10px 0;
    }//from   ww w. jav a  2s.c om
    iframe{
        width: 500px;
        border: 1px solid #a9a9a9;
    }
</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>



PreviousNext

Related