Liz Banach Liz Banach - 4 years ago 87
HTML Question

Append hash to iframe src with jQuery

I need to append a hash

#play
to the
src
attribute on an iframe element once a link is clicked.

Here is what I have attempted so far:

$(".portfolio-link").click(function() {
$('.first').append(.attr('src', '#play'));
});


Here is the link that I want to add
#play
to the
src
attribute on click:
<a href="#portfolioModal1" class="portfolio-link" data-toggle="modal"></a>


Here is the iframe element:
<iframe class="first" src="http://my.iframe.url" width="160" height="600" frameborder="0"></iframe>


How can I accomplish this with jQuery?

Answer Source

You could do a couple of ways. first, you could save the src as a variable and use that:

var myUrl = $('.first').attr('src');
$('.first').attr('src', myUrl + "#play");

or you could do it all in a single step:

$('.first').attr('src', $('.first').attr('src')+"#play" );

Either should work.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download