Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamic code to addClass to an element specified in the link
    primarykey
    data
    text
    <p>I'm trying to add a class to an element specified within my HTML from clicking on a button.</p> <p>HTML:</p> <pre><code>&lt;div id="servicenext"&gt;&lt;a href="#nav2"&gt;&lt;p class="four"&gt;next&lt;/p&gt;&lt;/a&gt; </code></pre> <p>Script:</p> <pre><code>$('#servicenext a').click(function(){ $('ul.navigation a').removeClass('active'); $(*the div in the href*).addClass('active'); }); </code></pre> <p>I'm hoping to have code that's dynamic. Basically it'll work by adding the class to any element specified in the <code>a</code> href. This is because i have quite a handful of these and do not wish to code them individually.</p> <p>EDIT:</p> <p>Thanks James! that's great! But I do have to apologize, i was confused myself at what I was asking for! What I'm actually looking to <code>addClass</code> to was actually the <code>li</code> in <code>ul.navigation a</code></p> <p>So if I'm thinking right, im trying to do this:</p> <ul> <li>by clicking onto the servicenext button, </li> <li>the code gets the <code>id</code> of the <code>div</code> in the <code>href</code></li> <li>the <code>addClass</code> is applied to the <code>li</code> with the same <code>href</code></li> </ul> <p>I'm guessing there should be if else statements somewhere, forgive me, i'm not really a programmer.</p> <p>the gibberish i came up with is:</p> <pre><code>$('#servicenext a').click(function(){ $('ul.navigation a').removeClass('active'); $($(this).attr("href").find($('ul.navigation li a').attr("href"))).addClass('active'); }); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    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. 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