We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17301
    • 932 Posts
    Hi guys,

    Potentially a silly question here but I have a one page site which I'm trying to keep fairly minimal and I'd like to be able to get the context of a resource on click. As far as I know I need to use AJAX to accomplish this but I've no idea where to start and tutorials don't seem to be offering me much help so far.

    I appreciate this is perhaps more of a jQuery question than MODX but I just wondered if you guys could offer any insight?

    Thanks,
      ■ email: [email protected] | ■ website: https://alienbuild.uk

      The greatest compliment you can give back to us, is to spend a few seconds leaving a rating at our trustpilot: https://uk.trustpilot.com/review/alienbuild.uk about the service we provided. We always drop mention of services offered by businesses we've worked with in the past to those of interest.
      • 3749
      • 24,544 Posts
      The easiest way would be to put the context in a div or span that's not visible because of its CSS class. Then a little JavaScript can just change toggle its class. Here's one way to do that: http://bobsguides.com/blog.html/2014/03/24/making-things-appear-and-disappear-without-jquery/.

      Actually, a better method is to just change the class of the item rather than toggling the attribute and control the visibility for the two classes with CSS.

      CSS
      span.span_hidden {
         display: none;
      }
      span.span_visible {
         display: inline;
      }




      HTML
      <span id="resource_context" class="span_hidden">[[*context_key]]</span>


      The JavaScript would change that line to:

      <span id="resource_context" class="span_visible">[[*context_key]]</span>
      




        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
        • 17301
        • 932 Posts
        Thanks for your reply, Bob.

        This method wouldn't really suit my purposes as I'm trying to keep the page weight down as much as possible and this piece of functionality I'd want to use it for is for a small library sample of about 20 books so it would be a lot of hidden articles.

        I think I've figured out a way to do it though and that's simply by creating the resources with the desired context and setting them to a blank template and then using the ajax .load to grab the context from the pages. I haven't put it into practice yet but I think thats the way to go.

        Thanks,
          ■ email: [email protected] | ■ website: https://alienbuild.uk

          The greatest compliment you can give back to us, is to spend a few seconds leaving a rating at our trustpilot: https://uk.trustpilot.com/review/alienbuild.uk about the service we provided. We always drop mention of services offered by businesses we've worked with in the past to those of interest.
          • 3749
          • 24,544 Posts
          That sounds good, though the JS for my suggestion is quite small and is a function that can be reused for every case. I think that would give you much faster responses than using Ajax. The initial page loads might be longer, but caching and the browser cache would help a lot with that.
            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