Arthur
Arthur

Reputation: 45

Provide link to iframe via js

I have an iframe with src but I want to provide the link to src via javascript on click of the button. How do I pass the value?

EDIT: The webpage refreshes everytime I click on the button. It displays the webpage and then refreshes it to display again. How to avoid this?

    <button type="button" onclick = "Open()">Click Me!</button>

    <iframe id="iframe"
  src="" //Provide link here
  style="
    z-index: 10;
    display: None;
  ">
</iframe>

<script>
    function Open() {
      let myIframe = document.getElementById("iframe");
        myIframe.style.display = 'block';
        iframe.src = "www.mylink.com"
    }
  </script>

Upvotes: 1

Views: 43

Answers (1)

dhazelett
dhazelett

Reputation: 335

you're not referencing myIframe when setting src in the example, you need to do myIframe.src = 'https://www.mylink.com'

Upvotes: 2

Related Questions