JavaScript for WordPress Forums Vanilla JavaScript Creating filter function loop

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #43494
    Angie Vale
    Participant

    I’m using Isotope to filter items with names beginning with A through to Z.
    Rather then creating 26 filter functions I would like to create a loop which loops through an array A to Z.
    This is what I have for the first 2 functions, I’m not sure how to declare my array A-Z and then loop through them to create a filter function for each letter of the alphabet.

    // filter functions
    var filterFns = {
    
    	// show if name begins with A
    	A: function() {
    		var name = $(this).find('.name').text();
    		return name.startsWith( "A" );
    	},
    	// show if name begins with B
    	B: function() {
    		var name = $(this).find('.name').text();
    		return name.startsWith( "B" );
    	}
    };
    #43508
    Zac Gordon
    Keymaster

    Hi Angie!

    Will the normal .sort() work?

    #43510
    Angie Vale
    Participant

    I only want to show the taxonomy terms beginning with each letter – so when you select A I want to show the taxonomy terms that begin with A, when you select B I want to show the taxonomy terms that begin with B, and so on. It is like an A-Z glossary that you can filter by letter. I have it working but only by creating 26 filter functions for A to Z. I am keen to learn if there is a way of doing this via a loop.

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.