Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>var idToClass = { 'aboutLink' : 'linkActive', 'sasLink' : 'link2Active' } $('#nav a').click(function(){ e.preventDefault(); $(this).addClass(idToClass[this.id]); }); </code></pre> <p><a href="http://jsfiddle.net/davidThomas/wqK8w/" rel="nofollow">JS Fiddle demo</a>.</p> <p>You could, instead, use <code>toggleClass()</code> to allow for those classes to be removed by a second click:</p> <pre><code>var idToClass = { 'aboutLink' : 'linkActive', 'sasLink' : 'link2Active' } $('#nav a').click(function(e){ e.preventDefault(); $(this).toggleClass(idToClass[this.id]); }); </code></pre> <p><a href="http://jsfiddle.net/davidThomas/wqK8w/1/" rel="nofollow">JS Fiddle demo</a>.</p> <p><strong>Edited</strong> in response to question, from the OP, in comments, below:</p> <blockquote> <p>How would I remove the class so that both links don't appear to be active at the same time?</p> </blockquote> <p>There's a few ways, but because you're adding different class-names to denote the 'active' state, they're a little inefficient. The first approach is to use a brute-force method, effectively looking for all <code>a</code> elements that have a <code>class</code> attribute and setting that attribute to the empty string, and <em>then</em> adding the <code>linkActive</code>/<code>link2Active</code> class-name to the clicked-on <code>a</code> element:</p> <pre><code>$('#nav a').click(function(e){ e.preventDefault(); var self = $(this); self.closest('ul').find('a[class]').attr('class', ''); self.toggleClass(idToClass[this.id]); }); </code></pre> <p><a href="http://jsfiddle.net/davidThomas/wqK8w/3/" rel="nofollow">JS Fiddle demo</a>.</p> <p>The alternative is to remove the specific classes from the elements who have their <code>id</code> listed in the <code>idToClass</code> object. This is, however, somewhat expensive in that it needs to iterate over the object, retrieving the <code>id</code>, finding the element <em>with</em> that <code>id</code> and <em>then</em> removing a <code>class</code>-name:</p> <pre><code>$('#nav a').click(function(e){ e.preventDefault(); for (var id in idToClass) { if (idToClass.hasOwnProperty(id)){ $('#' + id).removeClass(idToClass[id]); } } $(this).addClass(idToClass[this.id]); }); </code></pre> <p><a href="http://jsfiddle.net/davidThomas/wqK8w/4/" rel="nofollow">JS Fiddle demo</a>.</p> <p>If, of course, you use a common class-name then it all becomes much easier:</p> <pre><code>$('#nav a').click(function (e) { e.preventDefault(); var self = $(this); self.closest('ul') .find('.commonActiveClassName') .removeClass('commonActiveClassName'); self.addClass('commonActiveClassName'); }); </code></pre> <p><a href="http://jsfiddle.net/davidThomas/wqK8w/5/" rel="nofollow">JS Fiddle demo</a>.</p> <p>References:</p> <ul> <li><a href="http://api.jquery.com/addClass/" rel="nofollow"><code>addClass()</code></a>.</li> <li><a href="http://api.jquery.com/closest/" rel="nofollow"><code>closest()</code></a>.</li> <li><a href="http://api.jquery.com/event.preventDefault/" rel="nofollow"><code>event.preventDefault()</code></a>.</li> <li><a href="http://api.jquery.com/find/" rel="nofollow"><code>find()</code></a>.</li> <li><a href="http://api.jquery.com/removeClass/" rel="nofollow"><code>removeClass()</code></a>.</li> <li><a href="http://api.jquery.com/toggleClass/" rel="nofollow"><code>toggleClass()</code></a>.</li> </ul>
    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.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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