I have this code
;(function($, document, window) {
'use strict';
var defaults = {
el: '.na',
};
$.fn.baboy = function( options ) {
var settings = $.extend( {}, defaults, options );
this.each(function(){
var dis = $(this);
$('body').on('click', dis, function(e) {
var text = dis.text();
$( settings.el ).append('Click ' + text + '</br>' );
});
});
};
})(jQuery, document, window);
Then I wanted to use it on two elements
<a href="#" class="btn">Button 1</a>
<p class="duh"></p>
<a href="#" class="btn2">Button 2</a>
<p class="duh2"></p>
using the function in two elements
$('.btn').baboy({
el: '.duh',
});
$('.btn2').baboy({
el: '.duh2',
});
However, everytime I click any of the a tag, it seems like the other one also runs.
Here the fiddle to see the issue https://jsfiddle.net/xpvt214o/100340/
$('body').on('click', dis, function(e) {looks weird. The second argument toon()there should be a selector, but you are giving it a jQuery object. I'm not even sure why you are doing a delegate there. You already have the object you are trying to process events on.