0

Here's my relevant code:

var dropdown = {
  init: function() {
    $(".dropdown").click(".dropdown", dropdown.openDropdown, dropdown.secondFunction);
  },  
  openDropdown: function() {
    ...
  }
}

How can I call multiple functions on the click event? I added what I tried to do above.

EDIT:

So this is my new code with your guys help, and I can confirm both functions are being called because when I put alerts in they both trigger, but for some reason the code inside openDropdown right now isn't working. Is it because my $(this) references are off or something?

var dropdown = {
  init: function() {
    $(".dropdown").click(".dropdown", function() { dropdown.openDropdown(); dropdown.closeDropdowns(); });
  },
  openDropdown: function() {
    $(this).children(".dropdown-menu").show();
    $(this).addClass("open");
  },
  closeDropdowns: function() {
    //$(".open").removeClass(".open");
    //$(".open").children(".dropdown-menu").hide();
  }
}
1
  • $(".dropdown").click(".dropdown", ...); isn't right. Commented May 23, 2014 at 16:42

3 Answers 3

2

like this:

var dropdown = {
  init: function() {
    $(".dropdown").click(".dropdown", function(){ this.openDropdown(); this.secondFunction()});
  },  
  openDropdown: function() {
...
  }
}
Sign up to request clarification or add additional context in comments.

5 Comments

So will the .dropdown piece inside the click handler be passed to both functions?
on click 'this' will be element that event was invoked so yes '.dropdown'
Wouldn't this be the .dropdown element and not the dropdown variable?
Yeah-- I'm confused what this is and also what the .dropdown being passed in the click event is. Sorry...
this in the click event is the element. this in the scope of init would be dropdown itself.
1

You can do:

$('.dropdown').on('click', function () {
  dropdown.openDropdown();
  dropdown.secondFunction();
});

If you want this inside of openDropdown and secondFunction to be the element you would need to use .call.

var dropdown = {
  init: function() {
    $(".dropdown").on("click", function () {
      // The call makes `this` the element in the functions...
      dropdown.openDropdown.call(this);
      dropdown.secondFunction.call(this);
    });
  },  
  openDropdown: function() {
    console.log('openDropdown', this);
  },
  secondFunction: function() {
    console.log('secondFunction', this);
  }
};

dropdown.init();

1 Comment

Thank you so much Bill... I've never heard of that .call function, I'll read up on it.
1
var dropdown = {
  init: function() {
    $(".dropdown").click(".dropdown", function() {dropdown.openDropdown(); dropdown.secondFunction(); 
  });
  },
  openDropdown: function() {
     // first function callback
       },
  secondFunction: function()   {
    // second call back 
  }
};

dropdown.init();

DEMO

Comments

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.