Reputation: 4502
I would like to assign multiple events to a selector instead of creating separate event.
$('element').on('click resize scroll mouseover', function(){
// do something
});
The problem is that the resize event does not fire while the others do.
Upvotes: 2
Views: 126
Reputation: 459
$('element').on('click mouseover', function(e) {
// your code here
});
Upvotes: 0
Reputation: 21112
The documentation for on
states that the event argument is one or more space-separated event types and optional namespaces, such as click
or keydown.myPlugin
. So essentially, what you'r showing in your question is the answer itself.
In the next snippet you can see that clicking an moving the mouse over the element both trigger the listener. What you do inside there is entirely up to you
$(function() {
$("#target").on("click mouseover", function(ev) {
$("#output").text($("#output").text() + "\n"+"event triggered");
})
});
$(function() {
$("#target").on("click mouseover", function(ev) {
$("#output").text($("#output").text() + "\n"+"event triggered");
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="target">I'm a target</div>
<pre id="output">
</pre>
Upvotes: 1
Reputation: 51
space seperated is correct.
$('element').on('click resize scroll mouseover', function(){
// do something
});
But if the element is live one, you should use this statement.
$(document).on('click resize scroll mouseover', 'element', function(){
// do something
});
Upvotes: 0