computerguy
computerguy

Reputation: 177

Using .attr to change beginning of src property?

This changes

  1. "https://www.youtube.com/channel/blah" to
  2. "https://www.youtube.com/channel/blah/videos"
$(".yt-user-name.spf-link").attr("href", function(i, href) {  
    return href + '/videos';
});

I was wondering how I can change "https://www.youtube ..." to "https://www.m.youtube ..."

Possible solutions(?):

  1. How to replace element's attr href with each ? // strip url
  2. https://stackoverflow.com/a/179717/2038928

Upvotes: 0

Views: 60

Answers (2)

computerguy
computerguy

Reputation: 177

$(document).ready(function(){
    $('a').each(function(){
        this.href = this.href.replace('https://www.youtube.com', 'https://www.m.youtube.com');
    });
});

Upvotes: 0

Milind Anantwar
Milind Anantwar

Reputation: 82251

you can use .replace to replace the part of string:

$(".yt-user-name.spf-link").attr("href", function(i, href) {
  return href.replace("www.you","www.m.you") 
});

Upvotes: 3

Related Questions