1

I have a textbox which has the code;

<input type="text" maxlength="5" size="2" value="1" id="paging_textbox">

Now there is no encloding form tag on this page..

When user types in this textbox and presses Enter, the table on this page is updated (via AJAX call)

In Safari, the behavior is slightly different in the sense that even though it updates the table correctly, it kind of reloads the page (think it submits the form, but not sure how as there is no form tag) and resets the textbox value back to "1"

How do I prevent this from happening in Safari ?

Please help me. Thank you.

copenndthagen
  • 49,230
  • 102
  • 290
  • 442

3 Answers3

6

Just to be sure, terminate your callback function with a return false. That should do the trick.

 function(e) {
     e.preventDefault();
     // some awesome code
     return false;
 }

Oh, and another thing: in the future, to avoid such random behavior.. respect the standards, use a form :)

tXK
  • 712
  • 6
  • 11
  • 1
    Hey..just one thing...What exactly will the preventDefault or return false prevent in Safari...since i m not using a form OR not even clicking on a button, i do not think there is any Submit action to be prevented... – copenndthagen May 31 '11 at 16:46
  • 1
    well if you add `
    ` around that input AND get valid HTML, then there will be no problems at all :)
    – tXK May 31 '11 at 20:44
0

in jQuery

//Press Enter in INPUT moves cursor to next INPUT
$('#form').find(':input').keypress(function(e){
    if ( e.which == 13 )
    {
        $(this).next().focus();  //Use whatever selector necessary to focus the 'next' input
    }
});

JAVASCRIPT

<script language="JavaScript">
function disableEnterKey(e)
{
     var key;
     if(window.event)
          key = window.event.keyCode;     //IE
     else
          key = e.which;     //firefox
     if(key == 13)
          return false;
     else
          return true;
}
</script>

and write

<input type=”text” name=”mytext” onKeyPress=”return disableEnterKey(event)”>
xkeshav
  • 53,360
  • 44
  • 177
  • 245
0

use event.preventDefault() or return False should do the trick for you.

Abdul Kader
  • 5,781
  • 4
  • 22
  • 40