0

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

8
  • 2
    Just pass it as argument? app.open(this, e) and define .open to accept a second argument. Commented Sep 16, 2013 at 8:46
  • el refers to the element .main Commented Sep 16, 2013 at 8:46
  • @FelixKling didnt even know I could.. let me try Commented Sep 16, 2013 at 8:47
  • 2
    This code is confusing. In one place you are using e.target and in another el for the same thing. Be consistent, the references to el can be removed entirely and then you would call the function as app.open(e). Commented Sep 16, 2013 at 8:47
  • @Jon He uses el - jQuery(el).toggleClass('opened') Commented Sep 16, 2013 at 8:48

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah I didnt realize that el is an argument that passes.. well lets just say I learned and this is VERY useful information :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.