0

I have to the following function that I would like to modify so that it only binds the click event to all href's that = /ShoppingCart.asp?ProductCode="whatever" (whatever = whatever is in there") but not if it is specifically /ShoppingCart.asp?ProductCode="GFT". It must also check or convert a gft or Gft to upper case to check for those as well. So basically it has to check for any variation of the case of GFT. If it finds a "GFT" do not bind the click event.

function sacsoftaddtocart() {
    if (location.pathname == "/SearchResults.asp" || location.pathname == "/Articles.asp" || location.pathname.indexOf("-s/") != -1 || location.pathname.indexOf("_s/") != -1) {
        $("a[href^='/ShoppingCart.asp?ProductCode']").click(function () {
            var href = $(this).attr('href');
            addToCart3(href);
            return false;
        });
    }
}   
Reigel Gallarde
  • 64,198
  • 21
  • 121
  • 139
user357034
  • 10,731
  • 19
  • 58
  • 72

2 Answers2

1

You can do it using .toUpperCase() and .filter(), like this:

function sacsoftaddtocart (){
  if (location.pathname == "/SearchResults.asp" || location.pathname == "/Articles.asp" || location.pathname.indexOf("-s/") != -1 || location.pathname.indexOf("_s/") != -1) {
    $("a[href^='/ShoppingCart.asp?ProductCode']").filter(function() {
       return this.href.length - this.href.toUpperCase().indexOf('PRODUCTCODE=GFT') != 15;
    }).click(function () {
      var href = $(this).attr('href');
      addToCart3(href);
      return false;
    });
  }
}

You cant test it in a demo here. The this.href.length - matchPosition == 15 is checking that the ProductCode=GFT is both matched and there's nothing after the "GFT", so a product code like "GFT5" won't match.

Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • Worked perfectly and yes I didn't want the quote around the GFT but I understand what you did and fixed it. Thanks again for your help Nick, not the first time you've help me out!!! You da man!!! – user357034 Jul 17 '10 at 13:43
  • @user357034 - Welcome, I updated the answer for future users not wanting the quotes :) – Nick Craver Jul 17 '10 at 13:43
  • Does that mean if another product is GFT5 or 5gft it will bind? – user357034 Jul 17 '10 at 13:46
  • @user357034 - GFT5, yes it would, one sec I'll update it to handle those as well if that's a case you have. – Nick Craver Jul 17 '10 at 13:49
  • So let me clarify the only time a it should not bind is with any variation of gft, any case upper or lower of any and all letters. all other time it should bind if there is anything else in there including GFT in the product code. 5GFT5 should bind. – user357034 Jul 17 '10 at 13:54
  • @user357034 - GFT5, yes it would, one sec I'll update it to handle those as well if that's a case you have. – Nick Craver 5 mins ago No i don't want that. – user357034 Jul 17 '10 at 13:55
  • 1
    @user357034 - Correct, that's what the above updated code will do, click on the demo link underneath, you can add as many links to test as you want :) It'll **only** exclude "gft", case-insensitive, anything else gets bound. *For the last comment:* By "handle those" I meant property include/exclude them :) Before it was excluding anything *starting-with* gft, now it has to literally match *gft* (case in-sensitive), so "gft5" wasn't bound before, it is with the updated answer. – Nick Craver Jul 17 '10 at 13:56
  • So what you are saying is now it is set so that any of these will cause it to bind abcdGFTabcd gft123 123gFt – user357034 Jul 17 '10 at 14:04
  • i checked the jsfiddle and it appears so – user357034 Jul 17 '10 at 14:05
0

using the filter in this post link text

function sacsoftaddtocart() {
    if (location.pathname == "/SearchResults.asp" || location.pathname == "/Articles.asp" || location.pathname.indexOf("-s/") != -1 || location.pathname.indexOf("_s/") != -1) {
        $("a:regex('href','*/ShoppingCart.asp\?ProductCode=(!?=GFT)*)").click(function () {
            var href = $(this).attr('href');
            addToCart3(href);
            return false;
        });
    }
}

or if you don't want to use an exrat plugin:

function sacsoftaddtocart() {
    if (location.pathname == "/SearchResults.asp" || location.pathname == "/Articles.asp" || location.pathname.indexOf("-s/") != -1 || location.pathname.indexOf("_s/") != -1) {
        $("a['href^='/ShoppingCart.asp?ProductCode']")
            .filter(function(){ return !/ProductCode=GTF/.test($(this).attr('href')) };
            .click(function () {
                var href = $(this).attr('href');
                addToCart3(href);
                return false;
        });
    }
}

Try them and see what happens ;)

Community
  • 1
  • 1
Ian Wood
  • 6,515
  • 5
  • 34
  • 73