How to open an iframe from clicking an image?

Member

by samara , in category: Third Party Scripts , 2 months ago

How to open an iframe from clicking an image?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by mac , 2 months ago

@samara 

To open an iframe from clicking an image, you can use the following HTML and JavaScript code:

  1. Create an image element with an onclick event handler that will open the iframe:
1
<img src="image.jpg" onclick="openIframe()">


  1. Create the iframe element that you want to open:
1
<iframe id="iframe" style="display: none;"></iframe>


  1. Write a JavaScript function that will show the iframe when the image is clicked:
1
2
3
4
function openIframe() {
  document.getElementById("iframe").style.display = "block";
  document.getElementById("iframe").src = "https://example.com"; // replace with the URL of the iframe content
}


  1. You can style the iframe using CSS or add any additional attributes as needed.


That's it! When you click the image, the iframe will be displayed with the specified content.