4

Open submenu on click, close on next click - that's what i would like to achive. example is this page (submenu under 'follow' link).
it opens submenu (adds class 'open'), but not closing. stucked... :(

my html:

<ul id="toggle"><li>
<a href="#">Menu</a>
<ul id="dropdown" class="dropdown-menu" role="menu">
<li><a href="#">2017</a></li>
<li><a href="#">2012</a></li>
<li><a href="#">2003</a></li>
</ul>
</li>
<li><a href="#">Contact</a></li>
</ul>    

javascript:

$(document).ready(function(){
$('#toggle li').on('click', function(){
$(this).removeClass('open').addClass('open');
});
});

http://jsfiddle.net/Gallex/32pv6xz8/7/

3
  • 4
    7 answers so far, all suggest toggleClass, anybody left? Commented Apr 1, 2015 at 13:05
  • possible duplicate of jQuery toggle class Commented Apr 1, 2015 at 13:16
  • 1
    @Vohuman not all ;-), at least not only Commented Apr 1, 2015 at 13:20

7 Answers 7

11

You can use the function toggleClass() for this:

$('#toggle li').on('click', function () {
    $(this).toggleClass('open')
});

Demo

Here is a slightly different approach:

jQuery

$('#toggle li').on('click', function () {
     $(this).find('ul').slideToggle();
 });

CSS

#toggle li ul {
    list-style-type: none;
    left:0;
    position:absolute;
    display: none;
}

Demo 2

For preventing the redirect you have to use .preventDefault():

 $('#toggle li:has(#dropdown)').on('click', function (event) {
    if ($(event.target).parents('#dropdown').length > 0) {
        event.stopPropagation();
    } else {
        event.preventDefault();
    }
    $(this).find('ul').slideToggle();
});

I`m not sure if this is the cleanest or best approach, but it is working.

If you want to save the url for further use (e.g. redirectuing via window.location) you can assign the href-attribute to a variable:

var href = $(this).find('a').attr('href');

Demo 3

Reference

.toggleClass()

.slideToggle()

Sign up to request clarification or add additional context in comments.

8 Comments

i like very much this 'different approach' to my question. i will use it.
but, i would like to 'develope it further'. in my handwritten html code anchor tag doesn't have an url, but usually it does: '... <li> <a href="myhomepage.com/somecategory">Menu</a> <ul id="dropdown" class="dropdown-menu" role="menu"> ...' so, my question is: on click, how to 'force' to open a submenu not go to that url if anchor has an url?
@Gallex if one of the answers had helped you, feel free to accept any
a bit rushed, not brilliant yet. now all anchors not redirecting, but i need only this anchor which has submenu [link]jsfiddle.net/Gallex/0akdz4ow anchors in submenu, also need to redirect
yes, submenu anchors now working, but main menu do not jsfiddle.net/Gallex/0akdz4ow/4
|
2

Use toggleClass instead.

$(document).ready(function(){
  $('#toggle li').on('click', function(){
  $(this).toggleClass('open');
});
});

Comments

2

What you are looking for is the .toggleClass() function:

$(document).ready(function(){
    $('#toggle li').on('click', function(){
        $(this).toggleClass('open')('open');
    });
});

Check out the corrected jsfiddle :)

What you did wrong was, that you chained the add and remove functions:

$(this).removeClass('open').addClass('open');

What this will do is removing the class 'open' and (when this is finsihed) add the class 'open' again. This caused, that the class would not dissapear.

Comments

2
$(document).ready(function(){
  $('#toggle li').on('click', function(){
     $(this).toggleClass('open');
  });
});

use the toggleClass() function.

Demo

Comments

2

Use toggleClass() function in jquery

$('#toggle li').on('click', function () {
    $(this).toggleClass('open')
});

Demo Here

Comments

1

it's because you are removing the class then adding it...so it will ALWAYS be added

$(this).removeClass('open');

to close it .

Use this instead

$(document).ready(function(){
$('#toggle li').on('click', function(){
$(this).toggleClass('open');
});
});

Comments

1

Just do the following:

$(document).ready(function(){
    $('#toggle li').on('click', function(){
         $(this).toggleClass('open');
    });
});

jsfiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.