Michael Joseph Aubry
Michael Joseph Aubry

Reputation: 13492

How to refer to an event inside a jQuery object?

I am trying to refer to the event type here, I can refer to the element however. FIDDLE

var app = {

    open : function(el) {

                //Want to run this.. dont know how to refer to event properly
                if(jQuery(e.target).is('.cantOpen')){
                     e.preventDefault();
                     return;
                 }

                 jQuery(el).toggleClass('opened') 

         },
}

jQuery(document).ready(function() {

    //Prefer this
    jQuery('.main').click(function(e) {
            e.preventDefault();
            app.open(this)
    });

    //Ideally I dont want to add the code inside here but this does work
    jQuery('.main').click(function(e) {
            e.preventDefault();
            if(jQuery(e.target).is('.cantOpen')){
                e.preventDefault();
                return;
            }
            app.open(this)
    });


});

Hopefully you see my angle here... I would like to learn how to refer to the event type inside my open function? Is it possible? FIDDLE

Upvotes: 0

Views: 40

Answers (1)

PA.
PA.

Reputation: 29369

You can pass both the event and the element to the called function.

var app = {
    open : function(el,ev) {

and call it

$('.main').click(function(e) {
        e.preventDefault();
        app.open(this,e)

Upvotes: 1

Related Questions