Search code examples
javascripthtmljspstruts-1

How to avoid confirmation message from browser when form is submitted at onclick event of a span?


This is the initial state of the JSP page, with one set of data looks like this

enter image description here

If the client clicks on the + icon or Click here label the form should be submitted and the list will be increased which will look like

enter image description here

The form submission is proper when + icon is clicked, but when Click here label is clicked the browser is asking a confirmation message

enter image description here

The JSP code portion is :-

<table width="100%" cellspacing="1" cellpadding="2">
    <tr>
        <td align="center">
            <span style="cursor: pointer;" onclick= "addEvaluators('addEvaluators')">
                <font color="#228B22">Click here</font>&nbsp;
                <img src="images/admission/images/12673199831854453770medical cross button.svg.med.png" width="20px" height="20px" style="vertical-align:middle;">
            </span>
            to increase evaluators
        </td>                                               
    </tr>
    <logic:equal name="paperEvaluationUpdateEntryForm" property="showRemoveButton" value="true">
        <tr>
            <td align="center">
                <span style="cursor: pointer;" onclick="removeEvaluators('removeEvaluators')">
                    <font color="#B22222">Click here</font>&nbsp;
                    <img src="images/admission/images/15107-illustration-of-a-red-close-button-pv.png" width="22px"; height="22px"; style="vertical-align:middle;">
                </span>
                to decrease evaluators
            </td>
        </tr>
    </logic:equal>
</table>

The Javascript code I wrote is :-

function addEvaluators(method) {
    document.getElementById("method").value=method;
    document.paperEvaluationUpdateEntryForm.submit();
}

The reason why I kept both the image and label inside a span tag is because I want the form to be submitted at the onclick event of both and font tag is not having an onclick event.

My question is why that confirmation message is coming when the label is clicked, which is not happening when the image is clicked, and how to avoid it?


Solution

  • You can add below code in onSubmit method

    window.onbeforeunload=null;

    This will skip alert function.