Open In App

jQuery callbacks.has() Method

Last Updated : 24 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The callbacks.has() method in jQuery is used to answer whether the list has any callbacks attached. If a callback is passed as an argument, then it answers whether it is on the list or not.

Syntax:

callbacks.has([callback])

Parameters:

  • callback: The parameter defines the callback to search for, in the list.

Return Value: This method either returns true or false.

Example 1: This example returns “true” because ‘func’ is in the list.

html
<!DOCTYPE HTML>
<html>

<head>
    <script src=
        "https://code.jquery.com/jquery-3.5.0.js">
    </script>
</head>

<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>

    <p>
        jQuery | callbacks.has() method
    </p>

    <button onclick="Geeks();">
        Click Here
    </button>

    <p id="GFG"></p>

    <script>
        let el_down = document.getElementById("GFG");
        let res = "";
        let callbacks = jQuery.Callbacks();

        function Geeks() {
            let func = function (val) {
                res = res + "value passed is - " + val;
            };
            callbacks.add(func); // function added to list
            callbacks.fire("gfg_1");
            el_down.innerHTML = callbacks.has();
        } 
    </script>
</body>

</html>

Output:

jQuery-callbackshas()-method-

Example 2: This example returns “false” because ‘func2’ is not in the list.

html
<!DOCTYPE HTML>
<html>

<head>
    <script src=
        "https://code.jquery.com/jquery-3.5.0.js">
    </script>
</head>

<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>

    <p>
        jQuery | callbacks.has() method
    </p>

    <button onclick="Geeks();">
        Click Here
    </button>

    <p id="GFG_DOWN"></p>

    <script>
        let el_down = document.getElementById("GFG_DOWN");
        let res = "";
        let callbacks = jQuery.Callbacks();

        function Geeks() {
            let func1 = function (val) {
                res = res + "value passed is - " + val;
            };
            let func2 = function (val) {
                res = res + "value passed is - " + val;
            };

            // Function added to list
            callbacks.add(func1);
            callbacks.fire("gfg_1");
            el_down.innerHTML = callbacks.has(func2);
        } 
    </script>
</body>

</html>

Output:

jQuery-callbackshas()-method---2


Next Article

Similar Reads