How to display a iframe when a button is clicked via javascript?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • katiejeynes
    New Member
    • Dec 2017
    • 1

    How to display a iframe when a button is clicked via javascript?

    I have a content area in which I would like to display some text when the page loads and when a button is clicked (biography). When another button is clicked (AFT) I would like this text to be replaced by the content of a URL via an iframe.

    At the moment I have managed to get the text to appear when the page loads, however I can't get the iframe to replace the text when the AFT button is clicked. Can someone help me?

    HTML code:
    Code:
    <div id="content" > <script src="https://bytes.com/myJS.js"></script> <script> displayBiography(); </script> <iframe src="https://bytes.com/cwExample.pdf" name="iframe" id="iframe1"></iframe> </div> <div id="leftBar"> <br><br> <button class="button" onclick="displayBiography();"> Biography </button> <h3> Samples of Work </h3> <button class="button" onclick="toShow(#iframe1);" target="iframe1"> AFT </button> </div>
    JS code:
    Code:
    var content = document.getElementById("content");
    var biography = "<p> text here</p>"
    	    ;
    
    function displayBiography() {
    	"use strict";
    	content.innerHTML = biography;
    }
    	
    window.onload = function() {
    	var iframe = document.getElementById('iframe1');
    	iframe.style.display = 'none';
    }
    
    function toShow() {
    	var iframe = document.getElementsByName('iframe');
    	iframe.style.display = 'block';
    	content.innerHTML = iframe;
    }
  • RockybBalboa
    New Member
    • Feb 2018
    • 5

    #2
    You can do this from jQuery .html() method.

    On the button click you have show the iframe inside the div. THe code will be something like:

    Code:
    $("#buttonId").click(function(){
        $("#content").html("<iframe></iframe>");
    });

    Comment

    Working...