admin管理员组

文章数量:1398195

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);

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);
Share Improve this question asked Jun 12, 2013 at 16:27 Tom EllisTom Ellis 852 silver badges7 bronze badges 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./events. – Felix Kling Commented Jun 12, 2013 at 16:28
Add a ment  | 

2 Answers 2

Reset to default 9

You would want to structure it as

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

This will make it fire when $button1 is clicked.

Try this:

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

See documentation at api.jquery.

本文标签: jqueryJavaScriptDefine function without calling itStack Overflow