PDA

View Full Version : Enable pressing enter in a javascript form



Andy92
Mar 21st, 2007, 10:22 PM
Hi there,

I have...



<script type="text/javascript">
function handleKeyPress(e,form){
var key=e.keyCode || e.which;
if (key==13){
form.submit();
}
}
</script>


And then i have...



<input name=\"password\" type=\"password\" size=\"12\" maxlength=\"65\" class=\"account_form_text\" onkeypress='handleKeyPress(event,this.form)'/>


This will then enable submitting the form by pressing enter, but my form gets the data, so how do i do this?

I have tried putting form.get rather than form.submit but it doesnt work?

_Aerospace_Eng_
Mar 22nd, 2007, 02:37 AM
<script type="text/javascript">
function handleKeyPress(e,form){
var key=e.keyCode || e.which;
if (key==13){
document.forms[form].submit();
}
}
</script>
Try that. Note if you have any form elements with name="submit" in them it will not work. Though forms already submit by pressing enter so I think what you are trying is a bit redundant.

glenngv
Mar 22nd, 2007, 05:27 AM
<script type="text/javascript">
function handleKeyPress(e,form){
var key=e.keyCode || e.which;
if (key==13){
document.forms[form].submit();
}
}
</script>
Try that. Note if you have any form elements with name="submit" in them it will not work. Though forms already submit by pressing enter so I think what you are trying is a bit redundant.
this.form from a form element event handler already refers to the form the element belongs to. So the parameter form refers to the form object which makes form.submit() correct.

Allsortgroup, what exactly is your problem? Can you elaborate?