-2

I have a page with two sections map at the top and jQuery accordion menu at the bottom when I click accordion menu page is scrolling to top, I need the page should stay at the same place so I can see the jQuery accordion menu properly.

Is there anyway to prevent this? So that when I click on the menu, the page should stay at the same place in the browser?

menu.find('ul li > a').bind('click',function(event,ui){
    /*var ahref = $(event.currentTarget).attr('href');
    if(ahref!='#'){
        window.location.href = ahref;
    }else{*/

        var currentlink=$(event.currentTarget);
        if (currentlink.parent().find('ul.active').size()==1)
        {
            currentlink.parent().find('ul.active').slideUp('medium',function(){
               currentlink.parent().find('ul.active').removeClass('active');
            });
        }
        else if (menu.find('ul li ul.active').size()==0)
        {
            show(currentlink);
        }
        else
        {
            menu.find('ul li ul.active').slideUp('medium',function(){
                menu.find('ul li ul').removeClass('active');
                show(currentlink);
            });
        }
   /* }*/
});

to scroll accordion menu

andyb
  • 43,435
  • 12
  • 121
  • 150

2 Answers2

1

You need to add a return false or event.preventDefault() at the end of your click event function otherwise the browser will follow the <a href="#"> link you are clicking on by appending # to the page URI which instructs the browser to find the named anchor on the page. Since you are not supplying an anchor name (for example http://www.url.com/page# instead of http://www.url.com/page#anchorname) the browser jumps to the top of the page.

For example

menu.find('ul li > a').bind('click',function(event, ui){
    // your code

    // choose one of the following lines
    return false
    event.preventDefault()
});
andyb
  • 43,435
  • 12
  • 121
  • 150
0

You can use stopPropagation, event.preventDefault or return false...
Or just change the href to javascript:void(0).

Community
  • 1
  • 1
JoDev
  • 6,633
  • 1
  • 22
  • 37