Edan Maor
Edan Maor

Reputation: 10052

Getting the OnClick event when enter is pressed (IE)

I have the following code (inherited from someone):

<div class="feedback">&nbsp;</div>
   <form onsubmit="return false" autocomplete="off">
      Enter your guess: 
      <input type="text" size="48" id="guessedword"/>
      <input type="submit" value="Guess!" id="guessbutton"/>
   </form>
</div>

Now, I want to do something when the "Guess!" button is pressed, but I don't want the page to change (i.e. I don't want the form submitted). That's why the onsubmit is mapped to "return false". I also have the following JQuery:

$("#guessbutton").bind("click", onGuess);

The thing is, this works great in Firefox and Chrome. The only problem is IE. The guess button works correctly, the only problem is that when pressing Enter in the text field, the button isn't pressed, or at least the onClick behavior is never called. In Firefox and Chrome, it works great.

Is there any way to make this work the way I want it? Or am I going about this whole thing the wrong way? (I'm new to HTML, so I'm not even sure this is the right way to do things).

Thanks

Upvotes: 1

Views: 2609

Answers (2)

Artsiom Anisimau
Artsiom Anisimau

Reputation: 1179

Try to use onkeypress() event and handle enter key press. Dont use type="submit". Use type="button" instead. If you dont submit form to the server dont use it at all. Remove form tag from code.

<div class="feedback">&nbsp;</div>
      Enter your guess: 
      <input type="text" size="48" id="guessedword"/>
      <input type="button" value="Guess!" id="guessbutton"/>
</div>

document.onkeypress = function(){
   //your handling code here. keyKode of Enter key = 13
}

Upvotes: 0

raj
raj

Reputation: 3811

you can write :

<form onsubmit="onGuesS(); return false;" autocomplete="off">

This will not redirect enter to onclick, but in all the three browsers, Enter is equivalent to submitting the form.. so u write ur login in onSubmit and the return false that the page does not get submitted again.

Upvotes: 2

Related Questions