3

I'm looking for a way to find links with the "colorbox-load" class like this...

<a class="colorbox-load" href="http://www.youtube.com/watch?v=hYVGKdop63Y?fs=1&amp;width=640&amp;height=480&amp;hl=en_US1&amp;iframe=true&amp;rel=0">Kendra Grittani on stage at Koerner Hall (broken)</a>

...and using jquery I want to replace every instance in the href tag of "youtube.com/watch?v=" with "youtube.com/v/".

This is what I have so far, but I'm not sure how to do the find and replace.

$("a.colorbox-load").attr("href", "http://www.youtube.com") {
    var text = $(this).text();
    text = text.replace("youtube.com/watch?v=", "youtube.com/v/");
    $(this).text(text);
});

1 Answer 1

8

Use a simple string replace().

$("a.colorbox-load").each(function(){
  var newUrl = $(this).attr('href').replace('youtube.com/watch?v=', 'youtube.com/v/');
  $(this).attr('href', newUrl);
});

UPDATE

Created an example for you at jsFiddle.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, that makes sense. However, it's not working for me. I'm worried this might be because of something happening on my end so I'm going to play around with it for a bit more.
Updated my answer with an example. Try it out
Yup, it was my fault. This is what finally worked for me. (function ($) { $('a.colorbox-load').each(function(){ var newUrl = $(this).attr('href').replace('youtube.com/watch?v=', 'youtube.com/v/'); $(this).attr('href', newUrl); }); })(jQuery);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.