Skip to content Skip to sidebar Skip to footer

Jquery / Html - Execute Existing A-z Jquery Filter (of A Specific Letter) On Page Load, Based On Id/# In Url

Is it possible on page load to assign an active class based on the bookmark ID/#value in the page URL, to then execute an existing jQuery filter function? For example http://local

Solution 1:

Use window.location.hash to get the hash value from the url then use it as selector to match the id of the letter anchor

Something like:

var hash = location.hash;// will include the # prefix if there is oneif (hash && hash.indexOf('az-') > -1){
  $(hash).addClass('active')
}

Solution 2:

You can use localtion.hash parsing to achieve this.

$(function () {
        var _alphabets = $('.az-controls > a');
        var _contentRows = $('#inpageUL li:not(.no-result)');
        _alphabets.click(function () {
            var _letter = $(this), _text = $(this).text(), _count = 0;
            if(_text == 'All') _text = '.';
            _alphabets.removeClass("active");
            _letter.addClass("active");

            _contentRows.hide();
            _contentRows.each(function (i) {
                var _cellText = $(this).children('a').eq(0).text();
                if (RegExp('^' + _text).test(_cellText)) {
                    _count += 1;
                    $(this).fadeIn(400);
                }
            });
            if(_count === 0) $('.no-result').show();
            else $('.no-result').hide();
        });

        var hash = location.hash;
        if(hash && hash !== '#' && $(hash).length > 0) $(hash).click(); //will trigger click on a
    });

Post a Comment for "Jquery / Html - Execute Existing A-z Jquery Filter (of A Specific Letter) On Page Load, Based On Id/# In Url"