Jump to content

input image type used to trigger onsubmit-like javascript


chadmichael

Recommended Posts

I'm working some legacy code that makes us of a invisible image type input field to trigger some javascript when the form is submitted. It seems to work just like an onsubmit handler. Is this a total hack? What is the purpose of the image input type? Is this common usage? Is there any difference with using this or just using the form tag's onsubmit handler? I assume the guys, or gals, who wrote this must have had a reason, right?

<input type="image" border="0" src="/images/clear.gif" onclick="document.my_form.my_param.value = 'randomvalue';" />

Link to comment
Share on other sites

An image input type is if you want to use an image for a submit button. Using a transparent image just gives an invisible submit button.
Is it expected behaviour that the image type input tag's onclick handler will fire when the form is submitted through other means, such as hitting return on a input text field?
Link to comment
Share on other sites

The image type input is meant to be used to return X and Y coordinates from the image.For example, I had an idea of putting map locations on a database. I can put the image of the map into one of these inputs and then I can click on the region to send those X and Y coordinates to a database. Later on I can extract those numbers from the database and position a red X over the selected point on the map with CSS.

Link to comment
Share on other sites

The onclick handler will only fire if the button actually gets clicked on. When the form submits it fires its own onsubmit handler.
I don't have time to produce a clean test case of this, but I'm "pretty sure" that it's firing when you hit return in a text box . . . FWIW
Link to comment
Share on other sites

onsubmit will fire. If you have the image focused and hit enter, the onclick may fire too (not sure). Though, if they say have a text input focused and press enter, the onclick definitely won't fire (unless you manually call click()).

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...