Reputation: 93
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
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
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