Reputation: 1464
I have this code:
<input type="text" name="name" onkeydown="test(event)" />
function test(event){
if (event.keyCode == 13) {
$("#myButton").click();
return false;
}
}
It's not working as expected this way, but if I add an alert to the code like this:
function test(event){
alert("Why?!");
if (event.keyCode == 13) {
$("#myButton").click();
return false;
}
}
My button is this into gsp file:
<button type="submit" class="btn btn-primary" name="myButton" value="Submit">
Button
</button>
My form is this:
<g:form name="searchForm" controller="party" action="searchObject" class="form-horizontal margin-bottom-20">
</g:form>
Then everything works... Someone can tell me what's going on and how can I solve this problem?
Upvotes: 0
Views: 35
Reputation: 315
HTML:
<input type="text" name="name" onkeydown="test(event)" />
<button id="myButton">Button</button>
Javascript:
var test = function (event) {
if (event.keyCode == 13) {
$("#myButton").click();
return false;
}
}
$(function () {
$("#myButton").click(function () {
alert("This button was clicked using the enter key");
});
});
If i understood correctly, you are trying to invoke a click event with a key press (in this case, the enter key). Fiddle here: http://jsfiddle.net/jsu2jsx8
Upvotes: 1