Search code examples
jqueryreplacesplithref

jquery manipulate a couple of links


i have a couple of links i need to correct using some jquery. The links are formatted like this:

http://www.xyz.de/produkte/angebote/kategorie/angebote/produkte/singleview/produkt/oranier-wassertechnik-solar-pufferspeicher-set-2/

I want to split the href an get everything thats behind the last two slashes, while everything in front is being deleted.

My best shot was the following:

$('.tt-products-listhighlights a').each(function() {
  $(this).attr("href", function(index, old) {
        return old.replace("produkt/", "produkte/singleview/produkt/");
  });
});

Solution

  • i will go with split but there may be a better way

    $('.tt-products-listhighlights a').each(function() {
    
      var ary= $(this).attr("href").split("/");
      var lst=parseInt(ary.length)-2;
    
     ary[lst];//this is the last text
    
    });