Darius Enache
Darius Enache

Reputation: 15

How can I replace the youtube link and make it work?

I have tried to make a youtube interface to play music, but I want to change the music so I've made this script. The problem is, when I change the link the video won't work anymore and it gives me an error.

    <iframe id="iframe" width="500" height="315" src="https://www.youtube.com/embed/SHFTHDncw0g" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen>
</iframe>
<input type="text" name="link" id="link" size="40">
<input type="button" value="Play" onclick="changeLink();">
<script>
    function changeLink() {
        var changeLink = document.getElementById("link").value;
        document.getElementById("iframe").src += changeLink;
    }
</script>

Upvotes: 1

Views: 55

Answers (1)

Kirill
Kirill

Reputation: 180

You are adding a new link instead of replacing, because you used += instead of = in your Javascript. As a result, you have 2 links concatenated.

Try replacing:

document.getElementById("iframe").src += changeLink;

With this:

document.getElementById("iframe").src = changeLink;

Upvotes: 4

Related Questions