0

I'm trying to hide some unique links from a site's product listing page. Those links' href attributes endings are set in my "hrefs" array. I'd like to include only the endings (instead of the whole url-s), since the part before them are always the same.

The links which I want to hide from the site (in this case) are:

https://www.example.com/products/details/4123
https://www.example.com/products/details/4124
https://www.example.com/products/details/4130

My script is the next, the problem is it is only hiding the first match ( https://www.example.com/products/details/4123 ).

var hrefs = [ "4123", "4124", "4130", ];
var fullhref = ("https://www.example.com/products/details/") + (hrefs);
    $("[href]").filter(function(){
        return fullhref.indexOf(this.href) >-1;
    }).parent().hide();

So it can combine the first part of the url and the last part from the array and remove that link, but only the first one. I need some help how could the code check all of the given url endings (and hide all)?

JaSon
  • 188
  • 1
  • 2
  • 12
  • What do you expect `("https://www.example.com/products/details/") + (hrefs)` to do? – Amit Oct 09 '16 at 19:21
  • @Amit I want to create the full url with that line which the link is pointing to. That's how I managed to hide that product. Without the full href it could not find anything. – JaSon Oct 09 '16 at 19:23
  • @JaSon I think that there is a similar answer to your question. Check out this http://stackoverflow.com/questions/4758103/last-segment-of-url – ZombieChowder Oct 09 '16 at 19:25
  • @ZombieChowder I've seen that question some time earlier, that helped me to get the last part of the urls which I am storing in my hrefs array. – JaSon Oct 09 '16 at 19:31
  • You didn't answer my question at all – Amit Oct 09 '16 at 19:32

2 Answers2

1

What you have now would produce a string:

"https://www.example.com/products/details/4123,4124,4130"

Make a new array that combines the base url and the numbers array.

var fullhref =  hrefs.map(function(ref){
    return "https://www.example.com/products/details/" + ref
})

$("[href]").filter(function(){
    return fullhref.indexOf(this.href) >-1;
}).parent().hide();
charlietfl
  • 170,828
  • 13
  • 121
  • 150
1

you can modify the array with a loop then use it to filter the selection.

var hrefs = [ "4123", "4124", "4130", ];

for(var i = 0; i< hrefs.length; i++){
hrefs[i] = "https://www.example.com/products/details/" + hrefs[i];
}
Ali Somay
  • 585
  • 8
  • 20
  • This works great! Thanks a lot!! (Also needed to change the "fullhref" in my 4th line to "hrefs" -> `return hrefs.indexOf` ) – JaSon Oct 09 '16 at 19:48