Skip to content Skip to sidebar Skip to footer

Get Value Where Class Equals Active

RESOLVED It was determined that the issue is only on the platform we use thus all answers provided could be probably be right. Added another question regarding the same problem at

Solution 1:

From what I understand with your expected vs actual URL result, the problem should be in your replace method. Try with modified regex:

$(this).attr('href').replace(/\/search\?q=[^\&]*/, '/search?q=' + searchBran + '%20');

[^\&]* : it will look for all characters expect & (next set of query params)

Given that only one tab is active at a time (evident from code) and assuming that the second code block is also inside setActiveTab function block, you are only replacing the string /search?q=. This will be modifying the href as

/search?q=model1%20

Now clicking on model 2, will replace only the /search?q= portion, modifying to:

/search?q=model2%20model1%20

functionsetActiveTab(x) {
  /*for (i = 1; i < 6; i++) {
    $("#searchTab" + i).removeClass('active');
    searchURL = "";
    if (window.location.pathname.indexOf('specials') > -1) {
      collectionURL = "/collections/";
    } else {
      collectionURL = "/collections/";
    }
  }*/// refactoring the above loop with below line as it is not affecting the OP question
  $(".searchTab").removeClass("active");
  $(".Filter").find("input[type=text], textarea, select").val("");
  $("#searchTab" + x).addClass('active');
  if (x == 0) {
    searchBrand = ""
  } elseif (x == 1) {
    searchBrand = "model1"
  } elseif (x == 2) {
    searchBrand = "model2"
  } elseif (x == 3) {
    searchBrand = "model3"
  } elseif (x == 4) {
    searchBrand = "model4"
  } elseif (x == 5) {
    searchBrand = "model5"
  } //edit line//var searchBran = searchBrand;
  //end edit line//


  $('.homePageProducts a').each(function() {
    var newurl = $(this).attr('href').replace(/\/search\?q=[^\&]*/, '/search?q=' + searchBran + '%20');

    // only for display
    $("#newUrl").prepend("<p>" + newurl + "</p>");

    $(this).attr('href', newurl);
  });

  // goToByScroll();returnfalse;
}
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divclass="searchTabContainer col-xs-7"id="searchTabContainer"><divclass="searchTab"id="searchTab1"onclick="javascript:return setActiveTab(1);">Model 1</div><divclass="searchTab"id="searchTab2"onclick="javascript:return setActiveTab(2);">Model 2</div><divclass="searchTab"id="searchTab3"onclick="javascript:return setActiveTab(3);">Model 3</div><divclass="searchTab"id="searchTab4"onclick="javascript:return setActiveTab(4);">Model 4</div><divclass="searchTab"id="searchTab5"onclick="javascript:return setActiveTab(5);">Model 5</div></div><divclass="homePageProducts"><ahref="/link1/search?q=null&abc=123&xyz=456">Link 1</a><ahref="/link2/search?q=null&abc=222">Link 2</a></div><divid="newUrl"></div>

Post a Comment for "Get Value Where Class Equals Active"