3

I feel so so stupid for forgetting this, but I've been out of practice for a minute, and I'm drawing a blank.

Why is slideDown being called onload rather than when the click is handled?

function buttonClicked(buttonNumber) {
    $contentBox.slideDown("slow");
};

$button1.click = buttonClicked(1);
1
  • 4
    Because you are calling buttonClicked. The () after a function reference always calls the function. Example: function foo() { alert(42); }; foo();. Here foo is called because I put () after the variable name. Btw, if $button1 is a jQuery object, then you have to pass a function reference to the .click method, not assign a value to it. See learn.jquery.com/events. Commented Jun 12, 2013 at 16:28

2 Answers 2

9

You would want to structure it as

$button1.click(function() {
    buttonClicked(1);
});

This will make it fire when $button1 is clicked.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

$button1.click(function() {
    buttonClicked(1);
});

See documentation at api.jquery.com

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.