112233
112233

Reputation: 2466

return false doesn't stop page from reloading when enter key pressed

I've tried all my best for the past whole week to stop the page from reloading but to no avail.

This is the link for my site: www.sample.com

In the page, please scroll down and look for this (know a tutor) and key in any of names you find on the right.etc: keren, satish and press enter.If you click on 'Go' that works. But when press enter, the page reloads.I've even tried disabling all other scripts to see if any of them causing the page to reload, but even after removing links to all other scripts it still reloads.

enter image description here

My script for the above 'Know a tutor' when enter key is pressed.

$(function () {
        $('#tutor').keypress(function (event) {
            if (event.which == 13) {
              alert("enter pressed");                
              $.when(
              $.get('/search/tutor_name.php?name='+name,function(data){
                   globalStore.data = globalStore.data.concat(data);
                   console.log(data);
              },"json")
              ).then(function() {
              for (i = 0; i < globalStore.data.length; i++) {
              ...show stuff here....
             }
             //this is to stop page reload
              return false;
              });
         }
     });
    //this is to stop page reload
              return false;
});

Upvotes: 1

Views: 120

Answers (2)

Flakes
Flakes

Reputation: 2442

You have to return false from the block where you handle the enter keypress:

    $(function () {
        $('#tutor').keypress(function (event) {
            if(event.which == 13){
               alert("enter pressed");                
              $.when(
              $.get('/search/tutor_name.php?name='+name,function(data){
                globalStore.data = globalStore.data.concat(data);
                console.log(data);
              }
              ,"json")
              ).then(function() {
              for (i = 0; i < globalStore.data.length; i++) {
             console.log("in loop");
             }
              });
         return false;
         }
     });

});

Upvotes: 1

Prafulla Kumar Sahu
Prafulla Kumar Sahu

Reputation: 9693

Try to use event.preventDefault(); that may help you.

     $('#tutor').keypress(function (event) {
            event.preventDefault();
....................................

Upvotes: 1

Related Questions