This won't work:
$('#myElement').click(ElementClicked($(this)));
This is executing the function ElementClicked()
with whatever this
is at the time of writing, and binds the return value of ElementClicked()
to the click
event. Which in this case is nothing.
You'll need to pass a function to a click event, like so:
$('#myElement').click(function () { ElementClicked($(this)); });
This makes a(n anonymous) function, which will be bound to the click
event, and the function calls ElementClicked()
when run, passing this
. The function ElementClicked
can be defined as:
function ElementClicked(elem) {
elem.addClass('clicked');
}
As you will notice though, this
inside the function that is bound to the event will be the clicked element. So instead of making a function wrapper that calls a function passing the element, you can just abbreviate it like so:
$('#myElement').click(ElementClicked);
function ElementClicked() {
$(this).addClass('clicked');
}
Notice that the function is passed to click()
like a variable instead of being executed immediately, because there are no brackets ()
following it.
And BTW, you probably mean addClass
instead of addCss
.