Liz
Liz

Reputation: 1048

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?

Upvotes: 1

Views: 3002

Answers (2)

James B. Nall
James B. Nall

Reputation: 1458

var currentSrc = $('iframe.first').attr('src');
$('iframe.first').attr('src', currentSrc + '#play');

Upvotes: 1

Snowmonkey
Snowmonkey

Reputation: 3761

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.

Upvotes: 1

Related Questions