What is the use of event name with selector in jquery?
I have this html
<div class="wrapper">
<div class="holder">
<span class="pressy">press here..!!</span>
<div class="inner">
<span class="pressy">press here..!!</span>
</div>
</div>
</div>
and js
$('.wrapper').on('mousedown.inner','.pressy',function(e){
alert($(this).attr('class'))
})
In fact, I am getting a warning for both "press" since I can only get it for an internal push
and one more thing is using
'mousedown.inner'
(not that. I'm asking about general usage) how can I use it correctly?
script: http://jsfiddle.net/4ayqqfnm/
thank..
source to share
You just need to change the JS like this:
$('.wrapper').on('mousedown.inner','.inner .pressy',function(e){
alert($(this).attr('class'))
})
The persistence is mousedown.inner
optional (as @Arun says it applies to the event namespace) - it's not required here, but it doesn't hurt anything. The second part, .inner .press
y makes it a more specific selector and makes it ignore your first element.pressy
source to share
It is called a namespace and is used to individually target these events.
Suppose you add a few clicks to a button and then only want to remove one of those handlers, how do you do that? $('button').off('click')
will remove all click handlers added to the button that is not what we want.
So the solution is to use namespaces like
$('button').on('click.myevent', function(){...})
then if we do
$('button').off('click.myevent')
it will remove only those click handlers that are added to the namespace myevent
source to share
The only change you have to make is to specify a delegated selector that only cares about .pressy
internally .inner
, not each one .inner
.
$('.wrapper').on('mousedown.inner','.inner .pressy', function(e){
alert($(this).attr('class'))
});
The only thing that does .on('mousedown.inner', ...
is namespace your event handler, so you can remove them by namespace later without removing all other handlers mousedown
on .wrapper
.
source to share