Reputation: 43
I have a wordpress site which can be found here:
http://neocsatblog.mblx.hu/
On the right side you can see this links:
This urls camed from my youtube rss chanel (uploads playlist) which url is this:
https://www.youtube.com/feeds/videos.xml?channel_id=UCxnFBx6UhZS25Poa5-ZeeNg
I would like to modified this links for I can show videos in lightbox.
For example I would like to turn in to this link:
to this:
So I writed a script for it:
jQuery(document).ready(function() {
str = jQuery("#rss-4 ul li a").attr("href");
jQuery("#rss-4 ul li a").each(function() {
jQuery("#rss-4 ul li a").attr("href",str.replace(/(youtube.com)\/watch\?v\=|(youtu.be)\//g, "youtube.com/embed/"));
jQuery( '#rss-4 ul li a' ).attr( 'href', function(index, value) {
return value + '?autoplay=1';
});
//Lightview class need for the lightbox
jQuery("#rss-4 ul li a").addClass("lightview");
});
});
Unfortunetly this script dosen't do the right thing.
What it does?
The script replace all links url to the newest video url (the first item on the list).
In this case reaplace all url to
"Puzsér szár aranyért sem adná"
link url.
How can I fix this?
Upvotes: 0
Views: 64
Reputation: 171669
You are using each
to loop over all instances, however within each
you are setting all links to the same url.
Also, the value you store in str
is the href of the first matching selector.
this
within each
is the instance of the element collection so try:
jQuery("#rss-4 ul li a").each(function() {
var str = this.href;
str = str.replace(/(youtube.com)\/watch\?v\=|(youtu.be)\//g, "youtube.com/embed/");
str +='?autoplay=1';
// only change the current element
jQuery(this).attr("href", str);
});
--OR using attr(function)
jQuery("#rss-4 ul li a").attr('href',function(_, oldHref) {
var str = oldHref.replace(/(youtube.com)\/watch\?v\=|(youtu.be)\//g, "youtube.com/embed/");
str +='?autoplay=1';
return str;
});
Upvotes: 1