Skip to content

Instantly share code, notes, and snippets.

@ghernandez345
Created August 22, 2013 23:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghernandez345/6313851 to your computer and use it in GitHub Desktop.
Save ghernandez345/6313851 to your computer and use it in GitHub Desktop.
List menu that shows and hides related content.
/**
* Show the related content of an element. Just add `relation-list` to your `ul` that contains
* the elements that you will click. For all the elements related to the list items, just
* add class 'related-content'.
*
* (eg.)
* <ul class="content-list relation-list">
* <li></li>
* ....
* </ul>
*
* <div class="content-descriptions">
* <div class="related-content">....</div>
* <div class="related-content">....</div>
* <div class="related-content">....</div>
* ...
* </div>
*
* @param {DOM El} el [element of the item that was selected]
* @param {String} attr [name of the data attribute that we want to find]
* @param {String} context [selector string of the context to search for the list items
* and their related content. This allows us to use this pattern
* in any context.]
*/
module.exports = function showRelatedContent(el, attr, context) {
var $el = $(el),
$context = $(context),
$listItems = $('.relation-list li', $context),
$relatedContent = $('.related-content', $context),
value = $el.attr('data-'+attr);
$listItems.removeClass('selected');
$relatedContent.removeClass('show');
$el.addClass('selected');
$relatedContent.filter('[data-'+ attr +'="' + value +'"]').addClass('show');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment