So the code that I have so far is:
<fieldset id="LinkList">
<input type="text" id="addLinks" name="addLinks" value="http://">
<input type="button" id="linkadd" name="linkadd" value="add">
</fieldset>
It is not in a <form>
and is just as it is within a <div>
. However when I type something into the textbox
called "addLinks" I want the user to be able to press Enter and trigger the "linkadd" button
which will then run a JavaScript function.
How can I do this?
Thanks
Edit: I did find this code, but it doesnt seem to work.
$("#addLinks").keyup(function(event){
if(event.keyCode == 13){
$("#linkadd").click();
}
});
There are a js-free solution.
Set
type=submit
to the button you'd like to be default andtype=button
to other buttons. Now in the form below you can hit Enter in any input fields, and theRender
button will work (despite the fact it is the second button in the form).Example:
Tested in FF24 and Chrome 35 (
formaction
is html5 feature, buttype
is not).Based on some previous answers, I came up with this:
Take a look at the Fiddle
EDIT: If you dont want to add additional html elements, you can do this with JS only:
You could add an event handler to your input like so:
First of all add jquery library file jquery and call it in your html head.
and then Use jquery based code...