0

I have a submit button like as follows.

<input type="submit" 
       name="btnDelete" 
       id="btnDelete" 
       value="Delete" 
       onclick="this.form.action='${pageContext.request.contextPath}/Country/DeleteMany'"/>

I causes the form action to change, when it is pressed.


I however, need to invoke another function before this action is changed. This another function returns a boolean value based on a confirm dialog of JavaScript.

I have tried the following.

onclick="return confirmDeleteMuliple(); this.form.action='${pageContext.request.contextPath}/Country/DeleteMany'"

The action however, did not change on submit (when confirmDeleteMuliple() returns true).


I have tried placing the code that changes the action in a separate JavaScript function like as follows.

function deleteManyAction()
{
    document.dataForm.action='${pageContext.request.contextPath}/Country/DeleteMany';
}

And the onclick attribute is changed as follows.

onclick="return confirmDeleteMuliple();deleteManyAction();"

The deleteManyAction() function however, never invoked. It works only when the function confirmDeleteMuliple() is removed from onclick.

How to invoke these two functions in the defined sequence?

The expression ${pageContext.request.contextPath} evaluates to a context path of the application on load time like /Example (when this file (JSP) is parsed).

4
  • 1
    It seems you are looking for onsubmit event of javascript.. Commented Feb 10, 2014 at 14:56
  • Anyway, the action should be changed after confirmation (after confirmDeleteMuliple() returns true. Otherwise, nothing should happen). Commented Feb 10, 2014 at 14:58
  • Then you should call the next function in confirmDeleteMultiple function when it is returning true. Commented Feb 10, 2014 at 15:00
  • 1
    How do you not know what the return statement does? Commented Feb 10, 2014 at 15:03

2 Answers 2

1

Code placed after a return will never be reached. The return basically means stop working and give me what you have.

This should do what you want:

onclick="return submit();"

function submit(){
   if(confirmDeleteMuliple()){
       deleteManyAction();
       return true;
   }else{
       return false; //Otherwise the form will be submitted anyway.
   }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Try to keep code (JS) out of your presentation layer (HTML). I assign all of my event handlers in my JS files. Try this approach, which I think will make your code :

$("#btnDelete").click(function() {
    if ( confirmDeleteMuliple() ) {
       this.closest("form").attr("action", 
                                 ${pageContext.request.contextPath} + '/Country/DeleteMany');
       # Here, returning true will allow the form submission to complete with the new
       return true;
    } else {
       # This will stop the form from being submitted
       return false;
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.