Using an Image as a Submit Button in HTML

Posted by: twmeier

Tagged in: Javascript , HTML

There are times when you want to use an image as a button to submit a form. One case is when you need to use JavaScript to check the html form was submitted correctly. Here is the code:

    function checkForm()
        var msg = "Please correct the following: \n\n";
        var error = false;
        if(document.formName.elementName.value=="") {
            msg += "You must enter a date.\n";
            error = true;
            if(confirm("Are you sure you want to submit this form?")){                                               

<form name="formName" action="/index.php?&task=something" method="post">
    <input type="text" name="elementName" id="elementId" value="">

<a href="#" onclick="checkForm(); return false;"><img src="/images/brown_submit.jpg" /></a>

The method above works in all browsers, and is easy to use. When you click the image, the JavaScript function is called. If there are no errors in the form, the function submits the form. Its that easy.