I am using .on() function with this syntax:
$(".schemaTab").on("click", ".schema", function () {})
instead of .live()
because I am adding .schema
divs dynamically. Those divs have this code:
<div class="schema" id="SCH_16"> <img src="Img/btnRemove.png" class="ikona SCH_Remove" alt="Smazat" title="Smazat"> </div>
But I need to exclude the img from click event of its parent div, so any idea how to do it?
I tried:
$(".schemaTab").not(".SCH_Remove").on("click", ".schema", function () {})
...but it didn't work and I don't know how to add the .not()
inside the .on()
code
The not() is an inbuilt function in jQuery which is just opposite to the filter() method. This function will return all the element which is not matched with the selected element with the particular “id” or “class”. Syntax: $(selector).not(A) The selector is the selected element which is not to be selected.
jQuery :not() SelectorThe :not() selector selects all elements except the specified element. This is mostly used together with another selector to select everything except the specified element in a group (like in the example above).
jQuery not() Method The not() method returns elements that do not match a certain criteria. This method lets you specify a criteria. Elements that do not match the criteria are returned from the selection, and those that match will be removed.
.not() won't work because on
method attaches event handlers to the currently selected set of elements in the jQuery object and uses the original selector to test whether the event should apply.
So I reckon selecting and then filtering in your case will do the trick.
API: http://api.jquery.com/on/
you could try this:
$(".schemaTab").on("click", ".schema", function (e) { if (!$(e.target).is(".schemaTab")) { // not stuff in here } });
You can actually use the :not
selector with the .on
method, like so.
This is a cleaner solution than filtering results per Tats_innit's answer.
$('.schemaTab').on('click', '.schema:not(.booga)', function(event) { // Fun happens here });
I'm using jQuery 1.11.0.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With