Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
50
rated 0 times [  57] [ 7]  / answers: 1 / hits: 45519  / 9 Years ago, sun, march 8, 2015, 12:00:00

I am looking to add/remove a class from another div when a link is pressed. I have searched through several answers and searched online but could not find an answer that worked for me.



Here is my code:



<nav id=primary-menu> <a class=menu-toggle>Browse</a>
<ul>
<li><a href=#>Home</a></li>
<li><a href=#>News</a></li>
<li><a href=#>Rumors</a></li>
<li><a href=#>Tutorials</a></li>
<li><a href=#>Miscellaneous</a></li>
</ul>
</nav> <!-- end #primary-menu -->


I am looking to add the class active to #primary-menu when .menu-toggle is clicked.



For JS/jQuery, I've tried click, toggle, toggleClass, I've tried inlining the code, I've tried external scripts. Nothing seems to work and I'm not sure why.



I'd really appreciate your responses. PS: I'm not the best with JS/jQuery.


More From » jquery

 Answers
13

http://jsfiddle.net/te7brkmj/

this combination of 'click' and 'toggleClass' works for me.



$('.menu-toggle').click( function() {
$(#primary-menu).toggleClass(someClass);
} );


Try this:





$('.menu-toggle').click( function() {
$(#primary-menu).toggleClass(someClass);
} );

.someClass{
color: red;
}


   

<script src=https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js></script>

<nav id=primary-menu>

<a class=menu-toggle>Browse</a>

<ul>

<li><a href=#>Home</a></li>
<li><a href=#>News</a></li>
<li><a href=#>Rumors</a></li>
<li><a href=#>Tutorials</a></li>
<li><a href=#>Miscellaneous</a></li>

</ul>

</nav> <!-- end #primary-menu -->




[#67522] Friday, March 6, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
yulisa

Total Points: 436
Total Questions: 102
Total Answers: 123

Location: Palau
Member since Tue, May 30, 2023
1 Year ago
yulisa questions
Sun, Jun 20, 21, 00:00, 3 Years ago
Wed, Apr 14, 21, 00:00, 3 Years ago
Fri, Aug 7, 20, 00:00, 4 Years ago
Mon, Mar 23, 20, 00:00, 4 Years ago
;