kjdion84
kjdion84

Reputation: 10044

$(document).on() multiple specific events

I want to use $(document).on(), but I only want it to apply to 2 very specific events.

The events are when a form is submitted, or when a button is clicked. Both of these elements will have a data-ajax attribute.

To give you an idea what I need, here is code I tried which did not work:

$(document).on('form[data-ajax]:submit, button[data-ajax]:click', function (event) {
    alert('do stuff');
}

How do I get this to work for these very specific events?

Upvotes: 1

Views: 1690

Answers (3)

PeterKA
PeterKA

Reputation: 24638

You can define a function and call the function whenever each event fires like so:

function fireThisUponEvent( event ) {
    //your code goes here
}

$(document).on('submit', 'form[data-ajax]', fireThisUponEvent);

$(document).on('click', 'button[data-ajax]', fireThisUponEvent);

Upvotes: 0

Mohamed-Yousef
Mohamed-Yousef

Reputation: 24001

you can try

$(document).on('submit click','form[data-ajax] , button[data-ajax]', function (event) {
    event.preventDefault();
    alert('do stuff');
});

Demo Here

Upvotes: 3

JYoThI
JYoThI

Reputation: 12085

try this

$('#element').on('keyup keypress blur change', function(e) {
// e.type is the type of event fired
 });

Upvotes: 1

Related Questions