0

I have to trigger a function on 2 events. The first event is keyup (I know how to trigger this). The second event is click on a specific button.

Currently I know hot to trigger the first event as I said I use the following code:

emailPartner.on('keyup', function () {
    emailPartner.parent().removeClass('error-sign-up');
    $('.server').remove()
}

How do I trigger on both of the event?

2
  • Do you want to add your sample code? Commented Apr 19, 2015 at 10:03
  • @ Balázs Varga-I need a click on specific button I have Commented Apr 19, 2015 at 10:08

4 Answers 4

1

Adding multiple event listeners to element is possible by adding events separated by space as first argument to on

$('#emailPartner, #myButton').on('keyup click', function () {
    emailPartner.parent().removeClass('error-sign-up');
    $('.server').remove();
});

Assuming that your input id is emailPartner and button id is myButton on which you want to trigger these events.

See jQuery Docs: http://api.jquery.com/on/

Sign up to request clarification or add additional context in comments.

1 Comment

@Tushar- it doesn't get in to the function this way
0

You can do it in this way, if you don't specify a function inside an event, it will trigger it:

emailPartner.on('keyup',function(){
    emailPartner.parent().removeClass('error-sign-up');
    $('.server').remove()
}).click(function(){
    $(this).keyup();
});

Or like mentioned in another answer, you can set multiple events for a function using on():

emailPartner.on('click,keyup',function(){ //...

3 Comments

it doesn't get in to the function this way (on click)
Both of the solutions was always worked for me... I don't know why you have prblems with that... :/
this should work, but it's difficult to read as the chaining is completely unnecessary and provides no real function.
0

You have two elements, each with different events, right?

Take the event handler and place it in its own function, and attach it as the handler for both events.

var emailPartner = $('...'),
    myButton = $('...');

function myEventHandler() {
  emailPartner.parent().removeClass('error-sign-up');
  $('.server').remove()
}

emailPartner.on('keyup', myEventHandler);
myButton.on('click', myEventHandler);

Comments

0

You could try the following code :

emailPartner.on({
keyup:function () {...}, 
click:function() {....} 
}) ;

Update :

emailPartner.on('event1 event2 event3', function() {
...
}) ;

1 Comment

if this works, this is nice to group events on same element together. however, each event calls a different function. the OP wants the same function. granted they could call the same function, but it's an unnecesary duplicating of code reducing maintainability.

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.