2

I'm trying to handle multiple forms with ajax. Suppose I have #formA, and #formB. And in the submitForm() function, I have the following:

function submitForm() {
  var currentForm = $(this);
  if (currentForm == "#formA") {
   //do this
  }
  else if (currentForm == "#formB"){
   //do that
  } 
}

However this approach is not working. What is the best way to deal with this? Thanks in advance.

2 Answers 2

4

Try modifying your code:

function submitForm() {
  var currentForm = $(this);
  if (currentForm.attr("id") == "formA") {
   //do this
  }
  else if (currentForm.attr("id") == "formB") {
   //do that
  } 
}
Sign up to request clarification or add additional context in comments.

Comments

0

the code which you have written will not work, because you are trying to compare a jquery object with a string.

you have to use attr method of jquery to fetch the id of form and then compare it...something like this-

function submitForm() {
  var currentForm = $(this);
  if (currentForm.attr("id") == "formA") {
   //do this
  }
  else if (currentForm.attr("id") == "formB") {
   //do that
  } 
}

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.