Using an image as a submit button

The simplest way is to use an image as a submit button employing the <INPUT> HTML form tag.

<INPUT TYPE="IMAGE" SRC="butup.gif" ALT="Submit button">

When using this tag, you should remember some important differences between Netscape and Internet Explorer and how they handle this tag.

  • Netscape ignores the ALT attribute.
  • Netscape gives no indication that this image can be used to submit a form. In Internet Explorer, the mouse cursor changes to a hand.
  • Netscape ignores any JavaScript event handlers attached to this tag. Thus, client-side form validation is not possible using this tag in Netscape.

To overcome such browser incompatibility, I generally use a linked image and a little JavaScript code. The image when clicked submits the form through onClick() JavaScript event handler. Let me share this piece of code with you.

<A HREF="javascript:document.myform.submit()"
onmouseover="document.myform.sub_but.src='butdown.gif'"
onmouseout="document.myform.sub_but.src='butup.gif'"
onclick="return val_form_this_page()">

<IMG SRC="butup.gif"
WIDTH="143" HEIGHT="39" BORDER="0" ALT="Submit this form"
NAME="sub_but">

</A>

Click on the image below to see how it works.

Submit this form

The JavaScript code is actually quite simple once you break it down.
onclick() makes it possible to attach a function that validates the form and returns either true or false. If the return value is true, javascript:document.myform.submit() is executed, else the form is not submitted.
Furthermore, you notice that I’ve attached onmouseover and onmouseout event handlers to make your button more interactive.
When you mouse the mouse pointer over the image, the source (src) of image sub_but, which is located in myform form is changed to butdown.gif via onmouseover() and when the mouse pointer is removed from the image, the source is changed to butup.gif via onmouseout.
Move your mouse over the button to see the how it behaves and then click on it.

http://www.webdevelopersnotes.com/tips/html/21.php3

Leave a comment