Link to home
Start Free TrialLog in
Avatar of fairoakian
fairoakian

asked on

Javascript to submit form when you hit Enter

There is no HTML submit button, just an image.  I'm looking for the easiest (least amount of code) way to get the form to submit when somebody hits enter on this page.  Thanks.
Avatar of rlbalan
rlbalan

you will have to change the img tag to do that.

<img src="<ur imgsrc >" onClick="document.forms[0].submit()" />

PS : The assumption is that tyou only have one form in this HTML page
instead of using an <img src="whatever.jpg"/> tag have you considered using
<input type="image" src="whatever.jpg"/>
Avatar of fairoakian

ASKER

The image has some rollover effects applied to it.  I'm perfectly content to use an <input src=""> tag if the effects can be maintained.  I avoid javascript in my design and development, so I'll post what the designer added for the rollover.


 <script type="text/JavaScript">
<!--
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->
    </script>


and this for the button.


<a href="javascript:document.loginform.submit();"><img src="images/home_button_login.gif" width="129" height="19" border="0" id="Image1" onMouseOver="MM_swapImage('Image1','','images/home_button_login_hover.gif',1)" onMouseOut="MM_swapImgRestore()"></a>
ASKER CERTIFIED SOLUTION
Avatar of rlbalan
rlbalan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial