barakuda28
barakuda28

Reputation: 2902

How to pass the jQuery object which triggered an event to custom object function?

Please take a look at the following code:

jQuery(function($) {

Slider = function() {

    this.deleteSlider = function(e) {
        return false;
    }

}

var Slider = new Slider;

$('.heading-slider .actions .delete').click( Slider.deleteSlider );

});

I am using jQuery as you see. In this case how to pass the clicked element, wrapped in jQuery object to the deleteSlider function?

The only solution I could come up with is to use $(e.target), but there must be a better way.

Upvotes: 0

Views: 83

Answers (2)

prodigitalson
prodigitalson

Reputation: 60413

Silder.deleteSlider should be called with this pointing to the DOM element that triggered the click regardless of its original context, thus you dont need to do anything other than call $(this) from within the function.

If you want specifically call it with this referencing the Element wrapped with jquery then you could do:

$('.heading-slider .actions .delete').on('click', function(e) {
    Slider.deleteSlider.apply($(this), [e]);
});

Upvotes: 1

adeneo
adeneo

Reputation: 318182

Actually, there is'nt a better way as far as I know, and what's wrong with using $(e.target) ??

jQuery(function($) {
    Slider = function() {
        this.deleteSlider = function(e) {
            var $elem = $(e.target);
            return false;
        }
    }

    var Slider = new Slider;
    $('.heading-slider .actions .delete').on('click', Slider.deleteSlider);
});

Upvotes: 0

Related Questions