Calling a function in jQuery with click()

JqueryFunction

Jquery Problem Overview


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>

Jquery Solutions


Solution 1 - Jquery

$("#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);
});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEdward TanguayView Question on Stackoverflow
Solution 1 - JqueryTiagoView Answer on Stackoverflow