-2

How can I add "src" in iframe attribute (iframe src=" ") to getElementById (document.getElementById(" ").innerHTML = replaceurl) ?

<div class="output">
  <iframe src="" style="width: 640px; height: 480px;"></iframe>
</div>

<script>
  function myFunction() {
    var geturl = document.getElementById("url").value;
    var replaceurl = ("https://docs.google.com/gview?url=" + geturl + "&embedded=true");
    document.getElementById("").innerHTML = replaceurl;
  }
</script>
Ali Esmailpor
  • 1,209
  • 3
  • 11
  • 22
  • 2
    Very unclear what you are trying to ask here. Please go read [ask], and then _explain_ properly, what you want to achieve here. – CBroe Mar 02 '21 at 14:55
  • Does this answer your question? [How to set data attributes in HTML elements](https://stackoverflow.com/questions/13524107/how-to-set-data-attributes-in-html-elements) – Keimeno Mar 02 '21 at 14:56

3 Answers3

0

You must use the setAttribute method for this (https://developer.mozilla.org/de/docs/Web/API/Element/setAttribute).

In your case, it can be done by writing the following:

// HTML
<iframe id="your-iframe" src="" style="width: 640px; height: 480px;"></iframe>

// JS
document.getElementById("your-iframe").setAttribute('src', replaceurl);
Keimeno
  • 2,512
  • 1
  • 13
  • 34
0

why not trying to add an id to the iframe element and pass urls to it by calling your function? like this:

<div class="output">
        <iframe src="" id="myIframe" style="width: 640px; height: 480px;"></iframe>
    </div>

    <script>
        function myFunction(id) {
          var geturl = document.getElementById("url").value;
          var replaceurl = ("https://docs.google.com/gview?url=" + geturl + "&embedded=true");
          document.getElementById(id).src = replaceurl;
        }
//call your function.
myFunction("myIframe");
    </script>
Nabeel
  • 3
  • 5
-1

You can set the src of the <iframe> by directly assigning the new URL to the src property.

function myFunction() {
  const
    geturl = document.getElementById("url").value,
    replaceurl = `https://docs.google.com/gview?url=${geturl}&embedded=true`;
  document.querySelector('.output iframe').src = replaceurl;
}

myFunction();
<input type="text" id="url" value="https://stackoverflow.com/" />
<div class="output">
  <iframe style="width: 640px; height: 480px;"></iframe>
</div>
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132