Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In jQuery 1.7+, you should use <a href="http://api.jquery.com/on/" rel="nofollow">on</a>. The below examples binds the event to the <code>.filter_categories</code> element, working like a delegate event.</p> <p>A delegated-events approach attaches an event handler to only one element, the <code>.filter_categories</code>, and the event only needs to bubble up one level (from the clicked <code>li</code> to <code>.filter_categories</code>).</p> <p><strong>Method 1</strong> Assuming there is a sequential naming convention that you can use such as in your question:</p> <pre><code>$(".filter_categories").on("click", "li", function() { var articleClass = ".category-cat-" + ($(this).index() + 1); $(articleClass).fadeOut(); }); </code></pre> <p>Demo: <a href="http://jsfiddle.net/hadynz/yxjNb/1/" rel="nofollow">http://jsfiddle.net/hadynz/yxjNb/1/</a></p> <p><strong>Method 2</strong> Assuming that your different article names and corresponing articles will not have any particular pattern, you can use the following:</p> <pre><code>&lt;ul class="filter_categories"&gt; &lt;li&gt;&lt;a href="#" data-article="science"&gt;Cat 1&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#" data-article="geography"&gt;Cat 2&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#" data-article="math"&gt;Cat 3&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#" data-article="history"&gt;Cat 4&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;article class="science"&gt; Science Article &lt;/article&gt; ... $(".filter_categories").on("click", "li a", function() { $("." + $(this).data('article')).fadeOut(); }); </code></pre> <p>Demo: <a href="http://jsfiddle.net/hadynz/yxjNb/2/" rel="nofollow">http://jsfiddle.net/hadynz/yxjNb/2/</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload