popshuvit
popshuvit

Reputation: 110

have only one class name append to the end of href of an anchor tag?

var myClass;

jQuery(".option-set a").click(function() {
myClass = jQuery(this).attr("class");

jQuery("a.perma").each(function() {
    var _href = jQuery(this).attr("href"); 
    jQuery(this).attr("href", _href + "#filter=." + myClass);
});
});

Im using this code to append the class name of the filters to the end of the permalink for each thumbnail here

The issue Im running into now is that the class keeps getting assigned to the end of the permalink with each click so if I click on print then web then photography the url of the permalink would appear as: /#filter=.print#filter=.web#filter=.photography which still works, however it would be great if for the sake of tidiness it only displayed the last one.

Also once a thumbnail is clicked and the next page is loaded, I need the thumbnails to maintain the current permalink of the filter selected. Any ideas would be appreciated. I just cant seem to figure this out. I truly appreciate the help!!

Upvotes: 0

Views: 234

Answers (2)

algorhythm
algorhythm

Reputation: 8728

call it just once not on every click...

jQuery(document).ready(function() {
    myClass = jQuery('.option-set a').attr("class");

    jQuery("a.perma").each(function() {
        var _href = jQuery(this).attr("href"); 
        jQuery(this).attr("href", _href + "#filter=." + myClass);
    });

    jQuery(".option-set a").click(function() {...});
});

Upvotes: 1

Vytautas Butkus
Vytautas Butkus

Reputation: 5535

This modification should check if current class filter is not present.

jQuery("a.perma").each(function() {
  var _href = jQuery(this).attr("href"),
      newHref = _href.indexOf(myClass) === -1 ? _href + "#filter=." + myClass : _href;
    jQuery(this).attr("href", newHref);
  });
});

And to maintain filtered thumbnail urls on next page load you need to work with server and parse what parameters it passes or use Web storage to save current filtered path and set thumbnail hrefs accordingly on document load.

Upvotes: 0

Related Questions