views:

109

answers:

5

Hi all, I have a button , <input type="button" name="button"> when I click on it I submit a value from another input text <input type="text" name="btxt"> to a javascript that does some irrelevant action to this question,

Question: Is it possible that I trigger this javascript by pressing return on the keyboard instead of clicking on the button.

Note: These inputs are not inside form tags

A: 

Here is fully working example (tested on FF 3.5), use a JS library like jQuery to fire event if you need more portability.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>Onkeypress Test</title>
<script language="javascript">
function keyDowns(event) {
    if (event.keyCode == 13) {
     // If you are using jQuery just write this:
            // $('#testbutton').trigger('click');
            fireClickEvent(document.getElementById('testbutton'));
    }
}
// Event firing is not portable
function fireClickEvent(control) { 
    if (document.all) { 
        control.fireEvent("onclick"); 
    } else { 
        var clickEvent = window.document.createEvent("MouseEvent"); 
        clickEvent.initEvent("click", false, true); 
        control.dispatchEvent(clickEvent); 
    }
} 
</script>
</head>
<body onkeydown="keyDowns(event);">
<input type="button" name="button" value="Test" id="testbutton" onclick="alert('Clicked to me, you can do whatever')" />
</body>
</html>
eyazici
input type button has onkeydown?????
TeKapa
Why not? "onkeydown" is an event and if is supported by browser automatically supported by buttons.
eyazici
yeah, but you have to focus on the button and then press return to trigger it
TeKapa
Isn't this the usage scenerio? If not, bind keydown event to the body, and when user presses enter key from anywhere, fire buttons click event.
eyazici
but if you want focus on the button first, you dont even need to trigger onkeydown, you just need to press return and it will trigger the click
TeKapa
I have updated my post, I have tested it on FF 3.5, it may fail on other browsers, you can use a JS library like JQuery to fire event.
eyazici
A: 

I found this: Submitting Forms by Detecting Carriage Return / Enter Key Presses

This might work if you want to submit the form on Enter. This setup may not work for you though, because it looks like your input elements aren't in a form.

Adam Neal
+2  A: 

well, you can do it easily with form (which doesn't have to actually be submitted):

<form onsubmit="return yourAction();">
   <input type="text" name="text" />
   <button type="submit" name="button">Button</button>
</form>

and just return false; from yourAction()

krcko
he dont have form tags arround the input text
TeKapa
Upvoted this answer because this will also trigger the button if the text field is the active control, and that's usually what you expect. This method also makes it possible to add server side handling of the button click, so that you can support users that don't have JavaScript. I also believe that this solution is the best when accessibility of the web site.
Jan Aagaard
@TeKapa: Gandalf StormCrow did not say that it was not allowed and/or possible to add the form tag. I would recommend this solution over the one chosen as the answer.
Jan Aagaard
its better then the choosen one, i agree with you ;)
TeKapa
A: 

you dont need you input button.

Try it this way:

<input type="text" name="btxt" onkeypress="submitValue(this.value)">

function submitValue(value)
{
  var key;

  if(event.keyCode)
   key= event.keyCode;
  else if(event.wich)
   key=event.wich;

  if(key==13)
   callFunction(value);
}
TeKapa
input type="text"?
eyazici
yes.then when you type the value, press return and it will call the function you want
TeKapa
A: 

You need to trap the onkeydown event. I don't know where you want this event to extend to - the whole web page, or just a part?

Anyway, assuming that you want the onkeydown event to happen to everything in a div tag, e.g.

<div id="keyTrapper" onkeydown="trapReturn(event);">
    <input type="text" name="myText" id="myText" />
    <input type="button" name="button" id="myButton" onclick="alert(&quot;Clicked!&quot;);" />
</div>

The javascript function you need is:

    function trapReturn(event) {
        if (event.keyCode == 13) {
            var myButton = document.getElementById("myButton");
            myButton.click();
        }
    }
Mark Bertenshaw