Simulate button click on Return key press

  • Thread starter Thread starter Dan
  • Start date Start date
D

Dan

Consider a simple form with a textbox and button.

Is there a way to handle the common situation where a user presses "return"
rather than clicks on the button.

I can already detect the "return" key press using the textbox's onkeypress
event and now what I'm looking for is something like:

onkeypress="if 'return' key pressed, then button.click=True"

Any ideas?
 
Maybe I should try searching for Enter key first....



You can intercept the client side enter keypress event of the text box and
then click do what you want using javascript code.
Here's a good example:
http://www.kamp-hansen.dk/pages/showdoc.asp?id=28&menuid=21&menuid=18

Or you might try using this free control.
http://www.metabuilders.com/tools/DefaultButtons.aspx

And here's a good article on the subject:
http://www.allasp.net/enterkey.aspx

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
Hire top-notch developers at http://www.able-consulting.com
 
onkeypress="if(event.keyCode==13) document.getElementById('someButton').click()

or in other words just call the buttons click() method.

but I have no idea how to get the buttons id as the .NET framework changes all ids do something like _ctl1$_ctl4$someButton... naturally you can viewsource your page and see what id it generates and use that but that kinda hardcodes and if you change your control tree layout it might stop working.
im sure there's a way to get around this though...
 
Thanks for this.

Adrijan Josic said:
onkeypress="if(event.keyCode==13) document.getElementById('someButton').click()"

or in other words just call the buttons click() method..

but I have no idea how to get the buttons id as the .NET framework changes
all ids do something like _ctl1$_ctl4$someButton... naturally you can
viewsource your page and see what id it generates and use that but that
kinda hardcodes and if you change your control tree layout it might stop
working..
 
Back
Top