Search code examples
htmldefaultbutton

Set an image button to fire when enter key is pressed


Obviously I know how to do this with DefaultButtons within an ASP.NET web form. However, the way our client side developer wrote the code, he has a submit button done via javascript:

So the javascript is rendering the HTML.

<img id="submitBMI" onclick="quizCalc(); return false;" class="btnHover" src="Submit.gif">

Is there anyway to make this a DefaultButton?

Thanks guys.


Solution

  • If you mean to have the quizCalc() method be called when, for example, the enter key is pressed in a textbox of that form, then you could just set the onsubmit handler of the form to call that method:

    <form ... onsubmit="quizCalc(); return false;">
    

    If you want a little more control on which input elements call the method then you could look at using onkeypress with a single handler onKeyPress(event), check out a similar question

    Update

    You could do what Jonathan says, but just remove the return false as that cancels the keypress from adding characters to the textbox.

    document.onkeydown = function(e)
    {
      var keyCode = document.all ? event.keyCode : e.which;
      if(keyCode == 13) quizCalc(); 
    }