views:

164

answers:

2

I have a strange problem where an onclick event on an input image is being fired when i hit enter in input text box

<form id="MyForm" action="/someaction">

<input type="image" src="someimage.jpg" onclick="doStuff();$('#MyForm').submit();" />

<input type="text" name="textInput"/>

</form>

When the cursor is in the text box and i hit enter, rather than the form being submitted it calls the onclick event on the image input.

Any ideas whats going on ?

+1  A: 

I believe pressing the 'enter' button counts as a click in many cases, including links. If you want your action to be performed only when the mouse is used, then consider using onmousedown or onmouseup instead of onclick, I'd suggest the latter in your case.

Erik Vold
A: 

The onclick event is fired on input images when you hit enter. I'd wrapping an <img> in an <a>, as since you're separately calling submit(), the input element is made redundant.

susmits