Skip to content Skip to sidebar Skip to footer

Html Button Specify Selected

For the HTML element, how do I specify the button as 'selected' for the onclick to happen when the users clicks the enter key? I have a page with one button and that button isn't

Solution 1:

You can capture the enter keypress with 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();
}
}

Solution 2:

If using JavaScript is an option, you could do this:

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

It will "select" the button myButton.

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


Solution 3:

You could also place the the JavaScript on the onLoad event of the body tag, like this:

<!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>

Post a Comment for "Html Button Specify Selected"