2

I have the following block of code in Index.aspx. When I click on button the page, it reloads and shows nothing (empty page).

<div class="filter_block">
                <span>Show</span>
                <a href="#"><span class="title">full shedulle</span></a>
                <a id="buttonFindFilmByName" class="active"><span class="title">films name</span></a>
                <script type="text/javascript">
                    $(document).ready(function () {
                        $("#buttonFindFilmByName").click(function() {
                            $('#listInfoBlock').load('cinema/filmlist');
                        });
                    })
                </script>
            </div><!-- .filter_block-->

            <div id="listInfoBlock" class="list_infoBlock">           

            </div><!-- .list_infoBlock-->
ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
user348173
  • 8,818
  • 18
  • 66
  • 102

2 Answers2

6

Your page is reloading because that's the standard browser 's behavior when the user clicks on a a (link) HTML element.

You can stop this default behavior by using a parameter in the click handle and calling the preventDefault method:

$("#buttonFindFilmByName").click(function(e) {
    e.preventDefault();
    ....
Stéphane Bebrone
  • 2,713
  • 17
  • 18
1

Return false from the click handler function to tell the browser not to follow the link. This also allows you to set a real link in href for browsers that don't support JavaScript, or in case the user turned it off.

$(document).ready(function () {
  $("#buttonFindFilmByName").click(function() {
    $('#listInfoBlock').load('cinema/filmlist');
    return false; // <====== FIX
  });
});
kichik
  • 33,220
  • 7
  • 94
  • 114
  • Returning `false` [should be avoided](http://fuelyourcoding.com/jquery-events-stop-misusing-return-false/). – Stéphane Bebrone Dec 19 '11 at 07:38
  • Was not aware of that. Thanks for the heads up. Any idea if jQuery provides a compatibility layer for IE6/7 that don't support this? – kichik Dec 19 '11 at 07:50
  • 1
    Follow this [answer](http://stackoverflow.com/questions/1000597/event-preventdefault-function-not-working-in-ie-any-help), for IE you should use `event.returnValue = false`. – Stéphane Bebrone Dec 19 '11 at 07:55
  • I checked the latest [source](https://github.com/jquery/jquery/blob/master/src/event.js) and it seems to provide it. Not sure when they added it though. – kichik Dec 19 '11 at 08:23