Jones King
Jones King

Reputation: 63

Edit iframe src using jQuery

Why won't my code paste my iframe src? It is just empty in HTML.

<iframe id="Stream" src="" class="col-sm-12" frameborder="0" scrolling="no"></iframe>
<iframe id="Chat" src="" frameborder="0" scrolling="no"></iframe>
$('#Stream').attr("https://player.twitch.tv/?channel=" + channel, url)
$('#Chat').attr("https://www.twitch.tv/" + channel + "/chat?popout=", url)

Upvotes: 1

Views: 294

Answers (2)

Aligator
Aligator

Reputation: 43

Another method to change source path of iframe.

Please check it below.

$('#Stream').attr("src","https://player.twitch.tv/?channel=" + channel);
$('#Chat').attr("src","https://www.twitch.tv/" + channel + "/chat?popout=");

Upvotes: 1

Rory McCrossan
Rory McCrossan

Reputation: 337714

Your syntax for calling the attr() method is incorrect. The first argument is the name of the attribute and the second should be the value you want to set. It's also preferred to use the prop() method where possible, as it would be in this case.

$('#Stream').prop('src', "https://player.twitch.tv/?channel=" + channel);
$('#Chat').prop('src', "https://www.twitch.tv/" + channel + "/chat?popout=");

attr() method
prop() method

Note that this may still not work correctly if the requested domain has disallowed itself to be rendered in an iframe on a third-party website using X-Frame-Options.

Upvotes: 4

Related Questions