views:

311

answers:

2

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.

+1  A: 

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(); 
}
Steve Tranby
hmm that didn't seem to do it.
Jack Marchetti
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 Tranby
+1  A: 

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
I'm using jquery in some parts of the page. This didn't work, it didn't allow me to actually enter text into text boxes.
Jack Marchetti
Silly me. You would actually want to enter data in your form first, wouldn't you? Updated.
JonathanK