0

I have variables that have multiple functions in them. I then want to call these variables in a .click event. One works fine, but I want to have two, or even more. How can I do this? Below is the code I would expect to work.

var hideServices = function() {
            jQuery(".services-inner").css({"opacity": "0"});
            jQuery(".insignia-inner").css({"opacity": "0"});
            jQuery(".insignia-inner-text").css({"opacity": "0"});
};

var showMilitaryKit = function() {
            jQuery(".military-kit-inner").css({"opacity": "1"});
};

var showProperty = function() {
            jQuery(".property-kit-inner").css({"opacity": "1"});
};

    jQuery(".military-kit-hover").click(hideServices, showMilitaryKit);

    jQuery(".property-hover").click(hideServices, showProperty);

I'm convinced I haven't combined my variables in the .click event on the last line properly, but I can't find any documentation on what I want to achieve. Does anyone have a tweak that would work for me?

4
  • 3
    can you not just club the 2 functions into 1 ? Commented Nov 20, 2013 at 18:14
  • No. I'll amend my question so you can see why. Commented Nov 20, 2013 at 18:16
  • possible duplicate of Multiple Functions on a Single Event with JQuery/Javascript Commented Nov 20, 2013 at 18:16
  • I agree. It's the exact same question. Mine just goes into more detail with the question, but the answer is effectively the same. Commented Nov 20, 2013 at 18:44

2 Answers 2

5

Wrap the two calls in an anonymous function:

jQuery('.military-kit-hover').click(function() {
    hideServices();
    showMilitaryKit();
});

If you need to preserve the event object or this, do this:

jQuery('.military-kit-hover').click(function(e) {
    hideServices.call(this, e);
    showMilitaryKit.call(this, e);
});
Sign up to request clarification or add additional context in comments.

Comments

3
jQuery(".military-kit-hover").click(function() {
    hideServices();
    showMilitaryKit();
});

More in JQuery.click() documentation.

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.