Calling a function in jQuery with click()
In the code below, why does the open function work but the close function does not?
$("#closeLink").click("closeIt");
How do you just call a function in click()
instead of defining it in the click()
method?
<script type="text/javascript">
$(document).ready(function() {
$("#openLink").click(function() {
$("#message").slideDown("fast");
});
$("#closeLink").click("closeIt");
});
function closeIt() {
$("#message").slideUp("slow");
}
</script>
My HTML:
Click these links to <span id="openLink">open</span>
and <span id="closeLink">close</span> this message.</div>
<div id="message" style="display: none">This is a test message.</div>
Solution 1:
$("#closeLink").click(closeIt);
Let's say you want to call your function passing some args to it i.e., closeIt(1, false)
. Then, you should build an anonymous function and call closeIt
from it.
$("#closeLink").click(function() {
closeIt(1, false);
});