We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 27022
    • 47 Posts
    Looking for some assistance with a little JS issue that came up when integrating into MODX. The issue is that upon clicking the anchors, no change is being made to the page. However, if I open the anchor link in a new page, or manually paste and go using the generated anchor link, the filter does work, and so does the javascript animation.

    I have a filterable portfolio page, which worked fine before integration. Here is the HTML code that is giving me the issue:

    <ul id="portfolio-filter">
    	<li class="website"><a class="website" href="[[~[[*id]]? &scheme=`full`]]#website" rel="website">Websites</a></li>
    	<li class="logo"><a class="logo" href="[[~[[*id]]? &scheme=`full`]]#logo" rel="logo">Logos</a></li>
    	<li class="print"><a class="print" href="[[~[[*id]]? &scheme=`full`]]#print" rel="print">Print</a></li>
    	<li class="all"><a class="all" href="[[~[[*id]]? &scheme=`full`]]#all">All Work</a></li>
    </ul>
    
    <ul id="portfolio-list">
    	<li class="website"><a class="webwork" href="work-proshot.html" rel="fancybox" title="Proshot Directional Boring - Website Design"><img alt="Proshot Directional Boring" src="assets/images/work/proshot.gif" /> </a>
    
    	<p>Proshot Directional Boring</p>
    	</li>
    </ul>


    And here is the JS file, which is called in my html file:
    (function ($) {
        $.fn.filterable = function (settings) {
            settings = $.extend({
                useHash: true,
                animationSpeed: 1000,
                show: {
                    width: 'show',
                    opacity: 'show'
                },
                hide: {
                    width: 'hide',
                    opacity: 'hide'
                },
                useTags: true,
                tagSelector: '#portfolio-filter a',
                selectedTagClass: 'current',
                allTag: 'all'
            }, settings);
            return $(this).each(function () {
                $(this).bind('filter', function (e, tagToShow) {
                    if (settings.useTags) {
                        $(settings.tagSelector).removeClass(settings.selectedTagClass);
                        $(settings.tagSelector + ' [ href=' + tagToShow + ' ] ').addClass(settings.selectedTagClass);
                    }
                    $(this).trigger('filterportfolio', [tagToShow.substr(1)]);
                });
                $(this).bind('filterportfolio', function (e, classToShow) {
                    if (classToShow == settings.allTag) {
                        $(this).trigger('show');
                    } else {
                        $(this).trigger('show',  [ '.' + classToShow ] );
                        $(this).trigger('hide',  [ ':not(.' + classToShow + ')' ] );
                    }
                    if (settings.useHash) {
                        location.hash = '#' + classToShow;
                    }
                });
                $(this).bind('show', function (e, selectorToShow) {
                    $(this).children(selectorToShow).animate(settings.show, settings.animationSpeed);
                });
                $(this).bind('hide', function (e, selectorToHide) {
                    $(this).children(selectorToHide).animate(settings.hide, settings.animationSpeed);
                });
                if (settings.useHash) {
                    if (location.hash != '')
                        $(this).trigger('filter', [ location.hash ] );
                    else
                        $(this).trigger('filter', [ '#' + settings.allTag ] );
                }
                if (settings.useTags) {
                    $(settings.tagSelector).click(function () {
                        $('#portfolio-list').trigger('filter', [ $(this).attr('href') ] );
                        $(settings.tagSelector).removeClass('current');
                        $(this).addClass('current');
                    });
                }
            });
        };
    }(jQuery));
    $(document).ready(function () {
        $('#portfolio-list').filterable();
    });


    Here is the output coming from Chrome console. It doesn't appear until I click on one of the filterable anchors:

    Uncaught Error: Syntax error, unrecognized expression: [ href=http://localhost/msmdModX/work.html#website ]
    


    The way it should work, and does work outside of MODX, is that upon clicking "Websites", the non website portfolio thumbs disappear, resulting in only website related work being shown. Here is a link to the tutorial I used a while back on how to make my portfolio sortable http://www.newmediacampaigns.com/blog/a-jquery-plugin-to-create-an-interactive-filterable-portfolio-like-ours.

    I wasn't sure if this was an issue with FURL's, so I tried it both with and without, to no avail. I also wasn't sure if it had something to do with base href, etc. When I first integrated this page, I forgot to include any sort of url before the #website or #logo, so when I clicked to test the inks, the filtering occurred, before it redirected me to the homepage, which is what's leading me to thinking it has something to do with how I'm generating the url preceding the #websites. Could it be an issue with MODX and javascript misinterpreting something with all of the Square brackets? I'm not sure at this point....

    I've been staring at this for a while, so if I need to provide more details, information, or code, please let me know. Thanks!

    This question has been answered by multiple community members. See the first response.

    [ed. note: shick11sp last edited this post 11 years, 10 months ago.]
      • 3749
      • 24,544 Posts
      Is portfolio-filter a variable? You might want something like this:


      tagSelector: '#' + portfolio-filter + 'a',

      or this, if a is also a variable:

      tagSelector: '#' + portfolio-filter + a,



        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        No, "#portfolio-filter a" is a selector reflecting the ID attribute of the DOM element to act on.

        I downloaded their demo, and I immediately see one problem. The Javascript has this at line 25
        $(settings.tagSelector + '[href=' + tagToShow + ']').addClass(settings.selectedTagClass);
        


        The problem is that because of the way MODX handles URLs, you have to use the full URL, not just the hash tag. So instead of [href=#whatever] you end up with [href=page.html#whatever]. So you'll need to modify the tagToShow to strip off everything before the #.
          Studying MODX in the desert - http://sottwell.com
          Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
          Join the Slack Community - http://modx.org
        • discuss.answer
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          Got it. Add just before line 25
          tagToShow = tagToShow.substr(tagToShow.indexOf("#"));
          
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            Weird! It works fine in Firefox, but in Chrome I get the MODX error page. huh

            Ah, that's something else going on. That page gives the the error page in Chrome no matter what's in it. It's a test installation, so there's no telling what else I have going on with it.

            Ok, created a new resource, put the same thing in it, and now it works in Chrome as well.

            Also works in Safari. Attached are the files filterable.js and filterable.pack.js that I'm using. [ed. note: sottwell last edited this post 11 years, 10 months ago.]
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
            • discuss.answer
              • 27022
              • 47 Posts
              Quote from: sottwell at Nov 29, 2014, 11:44 PM
              Got it. Add just before line 25
              tagToShow = tagToShow.substr(tagToShow.indexOf("#"));
              

              This seemed to work great for me! Fixed my issue. Thanks Susan! I'm not sure of your post about the error page, I'm not seeing any MODX errors. Also, it takes away my console error that I was seeing in Chrome.
                • 28042 ☆ A M B ☆
                • 24,524 Posts
                Yeah, it was something else with Chrome. I have a lot of junk on that test installation, plugins and stuff, so maybe it's time to remove it and install a new one for testing.
                  Studying MODX in the desert - http://sottwell.com
                  Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                  Join the Slack Community - http://modx.org