Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

190
Views
Unable to fill src value using JS in iframe
<!DOCTYPE html>
<html>

<body style="text-align:center;">
    <iframe id="gfgFrame" src=""
        style="height: 50vh; width: 600px;">
    </iframe>

    <input type="text" id = "xxx">
  <p id = "u"></p>
    <button onclick="navigate()">
        Click it
    </button>

    <script>
    var url = document.getElementById("xxx");
        function navigate() {
            document.getElementById("gfgFrame").setAttribute("src", url);
      document.getElementById("u").innerHTML = "Hello";
    }
    </script>
</body>

</html>

If I enter a URL, it's not working. I have seen solutions on stackoverflow. I have also tried the other method, which is

    <script>
var url = document.getElementById("xxx");
       function navigate() {
            document.getElementById("gfgFrame").src
                = url;
        }
    </script>

Please forgive my intendation. Can anyone tell me where I'm going wrong?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You requesting element itself, not the value of this field, see:

const urlBlock = document.getElementById("xxx"); // get url text input

function navigate() {
  const iframe = document.getElementById("gfgFrame");
  const text = document.getElementById("u");
  
  // get url from input
  const url = urlBlock.value;
  
  iframe.setAttribute("src", url);
  text.innerHTML = "Hello";
}
<iframe id="gfgFrame" src="https://fr.wikipedia.org/wiki/Main_Page" style="height: 50vh; width: 600px;"></iframe>

<input type="text" id="xxx" value="https://wikipedia.org/wiki/Main_Page" />

<p id="u"></p>

<button onclick="navigate()">Click it</button>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!