Html button indicates selected

For an HTML element, how do I specify the "selected" button for onclick to happen when users press the enter key?

I have a page with one button, and this button does not appear when the page is displayed. I want the user to be able to press the Enter button without pressing the button.

 <button href="#" onclick="create_linkcard();" class="page_button"> <img src="Add.png" />Add</button> 
+1
html
Nov 26 '11 at 20:29
source share
3 answers

You can record the keystroke enter using the key codes.

 document.onkeypress = keyPress; function keyPress(e){ var x = e || window.event; var key = (x.keyCode || x.which); if(key == 13 || key == 3){ create_linkcard(); } } 
+2
Nov 26 '11 at 20:50
source share

If you use JavaScript, you can do this:

 <input type="submit" id="myButton"/> <script type="text/javascript"> document.getElementById("myButton").focus(); </script> 

He will β€œselect” the myButton button.

Make sure you place the <script> after the button element.

0
Nov 26 '11 at 20:38
source share

You can also put JavaScript in the onLoad event of the body tag, for example:

 <!DOCTYPE html> <html> <head> </head> <body onload="document.getElementById('pageButton').focus();"> <button id="pageButton" href="#" onclick="create_linkcard();" class="page_button"> <img src="Add.png" />Add</button> </body> </html> 
0
Nov 26 '11 at 21:25
source share



All Articles