simple expandable vertical menu, java script problem

I am trying to create a simple expandable menu in wordpress.
Bellow you can see the javascript I am using (copy and mix from internet).

<script type="text/javascript">
    $(document).ready(function($) {
        $('#access li').has('ul').addClass('has_submenu');
        $('#access ul li.has_submenu a').click(function() {
            $("#access ul ul").toggleClass("showHide");
        }); 
    });
</script>

The first part is ok, script adds has_submenu class to my menu.

Read More

Second part should toggle showHide class.
My problem is that when I click the showHide class appears but dissapears right after(page reloads).

Thanx for help 🙂

Related posts

Leave a Reply

1 comment

  1. Try this :

    <script type="text/javascript">
        $(document).ready(function($) {
            $('#access li').has('ul').addClass('has_submenu');
            $('#access ul li.has_submenu > a').click(function(event) {
                event.preventDefault();
                $("#access ul ul").toggleClass("showHide");
            }); 
        });
    </script>
    

    See event.preventDefault in the jQuery API.