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)?

5
  • What do you expect ("https://www.example.com/products/details/") + (hrefs) to do? Commented Oct 9, 2016 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. Commented Oct 9, 2016 at 19:23
  • @JaSon I think that there is a similar answer to your question. Check out this stackoverflow.com/questions/4758103/last-segment-of-url Commented Oct 9, 2016 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. Commented Oct 9, 2016 at 19:31
  • You didn't answer my question at all Commented Oct 9, 2016 at 19:32

2 Answers 2

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];
}
Sign up to request clarification or add additional context in comments.

1 Comment

This works great! Thanks a lot!! (Also needed to change the "fullhref" in my 4th line to "hrefs" -> return hrefs.indexOf )
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();

4 Comments

why do you use indexOf instead of ===?
@pwolaq what if it had a hash or query string also in the href?
then it should be this.href.indexOf(fullhref)
@pwolaq yup..I was definitely thing backwards. true answer is I just copied what OP had

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.