Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I prevent scrolling to the top of a page when popping up a jQuery UI Dialog?

I currently use jTemplates to create a rather large table on the client, each row has a button that will open a jQuery UI dialog. However, when I scroll down the page and click on one of those buttons, jQuery dialog will open, but the scroll position get lost and the page jumps back to the top (with the blocking and the actual dialog showing off the screen). Has anyone seen or know what might cause this problem?

Thanks.

like image 418
Kiwik Avatar asked Jul 20 '09 21:07

Kiwik


People also ask

How to prevent page from scrolling to top in jQuery?

So, with jQuery, you can alternatively use this approach to prevent the default link behaviour: $('#ma_link'). click(function(e) { doSomething(); return false; }); If you're using raw DOM events, this will also work on modern browsers, since the HTML 5 spec dictates this behaviour.

How to disable background scrolling when popup modal is open in jQuery?

Approach: A simple solution to this problem is to set the value of the “overflow” property of the body element to “hidden” whenever the modal is opened, which disables the scroll on the selected element.


2 Answers

Are you using an anchor tag to implement the "button" that pops the dialog? If so, you'll want the click handler that opens the dialog to return false so that the default action of the anchor tag isn't invoked. If you are using a button, you'd also need to make sure that it doesn't submit (by returning false from the handler) and completely refresh the page.

For example,

$('a.closeButton').click( function() {      $('#dialog').dialog('open');      return false; });   <a class='closeButton'>Close</a> 
like image 101
tvanfosson Avatar answered Sep 18 '22 16:09

tvanfosson


If your buttons work with an html anchor tag with href="#" replace the href for example by href="javascript:;" or any other method that you use to disable the href. The reason why the scrolling happens is because of href="#" scrolls to the top of your page.

like image 43
Frans Van Assche Avatar answered Sep 19 '22 16:09

Frans Van Assche