If you have a normal form:
<form method='post' action='#'>
<input type='text' name='#' />
<input type='submit' value='Submit />
</form>
Then you can fill in the input field, and press enter. However, I am using a link to do my work so:
<form method='post' action='#'>
<input type='text' name='#' />
<a href='#' class='button' onclick='get_form(this).submit();'>Submit</a>
</form>
That works like a charm, however, I want it to inherit the 'enter-clickable' that the normal submit field has. Is that possible through some javascript?
If you have a normal form:
<form method='post' action='#'>
<input type='text' name='#' />
<input type='submit' value='Submit />
</form>
Then you can fill in the input field, and press enter. However, I am using a link to do my work so:
<form method='post' action='#'>
<input type='text' name='#' />
<a href='#' class='button' onclick='get_form(this).submit();'>Submit</a>
</form>
That works like a charm, however, I want it to inherit the 'enter-clickable' that the normal submit field has. Is that possible through some javascript?
Share Improve this question asked Feb 18, 2012 at 15:52 DennisDennis 9494 gold badges16 silver badges31 bronze badges 2- This might help: stackoverflow./questions/477691/… – Tim Commented Feb 18, 2012 at 16:04
-
1
Why are you using a link when a button already does what you want it to do? If it's for style purposes, you could use a
<button>
element and style it to look like a link, but it would still be "enter-clickable" without JavaScript. – Brandan Commented Feb 18, 2012 at 16:51
4 Answers
Reset to default 5$('#your_input_textfeild_id').keypress(function(e) {
//check if enter is pressed
if(e.keyCode == 13) {
//click your link
$('#your_submit_link_id').click();
}
});
try this:
<script>
document.getElementById('TEXTBOXID').onkeypress = function(e){
var evt = e || window.event;
if(evt.keyCode == 13){
get_form(this).submit();
}
}
</script>
I think the correct way to do this is to actually include a
<input type="submit" >
which you can then style to be hidden. Alternatively, just style the submit element to look the way you want. My reason for this is that the browser looks for that submit element and that's how the enter key gets assigned to submit the form. And I somehow think it's better to let the browser handle matters like that rather than using JavaScript to capture a keypress event.
You may try this
document.getElementById('my_input').onkeypress = function(e)
{
var event = e || window.event;
if(event.keyCode == 13 && this.value!="")
{
form = this.parentNode;
if(form.tagName.toLowerCase() == "form")
{
form.submit();
}
}
}
You have to add an id to your text box and replace my_input with it.