5 Replies - 5704 Views - Last Post: 14 July 2012 - 10:02 PM

#1 kimmi_baby  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 87
  • Joined: 18-April 11

How to make this accordion menu for wordpress close when clicked

Posted 13 July 2012 - 08:54 PM

Hi,

I have an accordion menu for wordpress that works nearly the way I would like it to. I was wondering if there's a way to make the menu close once clicked again. I'm happy with how it opens when clicked, it just stays open after that and can't be closed by clicking again.

$(document).ready( function($) {
    // This hides all the sub menus on page load
    $('#nav-main ul.sub-menu').hide();
    // This makes sure that all the sub menus are open when applicable
    $('#nav-main li.current-menu-item').children().show();
    // This keeps the nav open to the item that you are navigating to.
    $('#nav-main li.current-menu-item').parents().show();
    $('#nav-main ul li a').click(
    function() {
    var checkElement = $(this).next();
    if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
    return false;
    }
    if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
    $(this).parent().siblings("li:has(ul)").find("ul").slideUp('normal');
    $('#nav-main ul ul li ul:visible').slideUp('normal');
    checkElement.slideDown('normal');
    return false;
    }
    }
    );
    $('#nav-main ul ul li a').click(
    function() {
    var checkElement = $(this).next();
    if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
    return false;
    }
    if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
    $('#nav-main ul ul').slideUp('normal');
    $('#nav-main ul ul li ul:visible').slideUp('normal');
    checkElement.slideDown('normal');
    return false;
    }
    }
    );
    });




Appreciate any help!

Is This A Good Question/Topic? 0
  • +

Replies To: How to make this accordion menu for wordpress close when clicked

#2 exiles.prx  Icon User is offline

  • D.I.C Head

Reputation: 65
  • View blog
  • Posts: 239
  • Joined: 22-November 10

Re: How to make this accordion menu for wordpress close when clicked

Posted 14 July 2012 - 06:20 PM

$('#nav-main ul ul li a').click(


is within the document.ready function so it won't execute when the item is clicked on. You should place code within document.ready that you want to execute when the document is loaded and items that will execute based on an event outside the document.ready function.

This post has been edited by exiles.prx: 14 July 2012 - 09:53 PM

Was This Post Helpful? 0
  • +
  • -

#3 kimmi_baby  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 87
  • Joined: 18-April 11

Re: How to make this accordion menu for wordpress close when clicked

Posted 14 July 2012 - 06:28 PM

Thank you for you reply. How do I fix this? I actually found the code in a tutorial for accordion menus.
Was This Post Helpful? 0
  • +
  • -

#4 exiles.prx  Icon User is offline

  • D.I.C Head

Reputation: 65
  • View blog
  • Posts: 239
  • Joined: 22-November 10

Re: How to make this accordion menu for wordpress close when clicked

Posted 14 July 2012 - 06:29 PM

Move the items that will execute based on an event outside the document.ready function.

This post has been edited by exiles.prx: 14 July 2012 - 09:53 PM

Was This Post Helpful? 0
  • +
  • -

#5 kimmi_baby  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 87
  • Joined: 18-April 11

Re: How to make this accordion menu for wordpress close when clicked

Posted 14 July 2012 - 08:16 PM

Thank you for your help so far.. But I'm not too sure what you mean. My javascript skills aren't great.
Was This Post Helpful? 0
  • +
  • -

#6 exiles.prx  Icon User is offline

  • D.I.C Head

Reputation: 65
  • View blog
  • Posts: 239
  • Joined: 22-November 10

Re: How to make this accordion menu for wordpress close when clicked

Posted 14 July 2012 - 10:02 PM

After refreshing my memory with document.ready() and events, you can bind events in document.ready() so this is not the issue. I would suggest posting this issue in the jQuery section as you will get better responses there since this is more of a jQuery question.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1