Kater89
Kater89

Reputation: 93

Combine jQuery Functions

I'm sure that this is somewhere but I am wondering if I can combine these two jQuery functions (assuming that 'DO SOMETHING' is the same code).

$( window ).resize(function() {  
    //DO SOMETHING
});

$('.mySelector input').on('click', function() {
    //DO SOMETHING
});

Sort of like in php when you use ||

Every time that I search for this, it comes up with results for $('#myIdOne, #myIdTwo') which won't help me.

Thanks!

Upvotes: 1

Views: 24

Answers (2)

gaetanoM
gaetanoM

Reputation: 42044

To merge what you asked you need to do like:

$(window, '.mySelector input').on('click resize', function(e) {
  if (e.target === window) {
    alert('Event comes from window');
  } else {
    alert('Event comes from: ' + e.target.tagName)
  }
});

Upvotes: 0

cFreed
cFreed

Reputation: 4474

You don't need to combine anything to achieve what you want.
Merely you have to call the same function in both events.

$( window ).resize(doSomething);

$('.mySelector input').on('click', doSomething);

function doSomething() {
    //DO SOMETHING
});

Upvotes: 3

Related Questions