Tom Ellis
Tom Ellis

Reputation: 85

JavaScript - Define function without calling it?

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

Upvotes: 3

Views: 3096

Answers (2)

Paul Oliver
Paul Oliver

Reputation: 7681

Try this:

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

See documentation at api.jquery.com

Upvotes: 0

ayyp
ayyp

Reputation: 6598

You would want to structure it as

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

This will make it fire when $button1 is clicked.

Upvotes: 9

Related Questions