0

I am trying to do a similar action when submitting two different forms:

I am able to do this using :

 $('#form1,#form2,').on('submit', function (e) {
                    e.preventDefault();
                    //Get action URL 
                    serviceUrl = $(this).attr('action')
                   //Dome something
                });

But is there a way to have multiple selectors when jQuery object is already declared?

var form1=$('#form1');
var form2=$('#form2');

 $($form1,$form2).on('submit', function (e) {
                e.preventDefault();
                //Get action URL 
                serviceUrl = $(this).attr('action')
               //Dome something
            });
2
  • 1
    You can use .add() method i.e. $form1.add($form2).on(....) Commented Oct 30, 2018 at 6:20
  • Thanks @Satpal, It solved my problem , if you could add this to answer , I will mark that as solution. Commented Oct 30, 2018 at 6:26

1 Answer 1

1

You can use .add() method

Create a new jQuery object with elements added to the set of matched elements.

$form1.add($form2).on(...)
Sign up to request clarification or add additional context in comments.

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.