1

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.

Jack Marchetti
  • 15,536
  • 14
  • 81
  • 117
  • Is what you're after having the enter key pressed inside a textbox submit to the method quizCalc()? If so, does it work in any of the different browsers? – Steve T Jan 06 '10 at 21:58

2 Answers2

2

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(); 
}
Community
  • 1
  • 1
Steve T
  • 7,729
  • 6
  • 45
  • 65
  • hmm that didn't seem to do it. – Jack Marchetti Jan 06 '10 at 21:38
  • also, if you use the form onsubmit make sure you get rid of any onkeydown/onkeypress/onkeyup code relating to the [Enter] key (keyCode 13) as I believe the onsubmit makes more sense than capturing a key anywhere on the page, just my 2c – Steve T Jan 06 '10 at 22:17
2

Assuming you aren't using a js library, this will work if enter is pressed anywhere on the page:

document.onkeydown = function KeyDown(e)
{
  var keyCode = document.all ? event.keyCode : e.which;

  if(keyCode == 13) {
    quizCalc(); 
    return false;
  }
}
JonathanK
  • 3,000
  • 1
  • 20
  • 20