The Wayback Machine - https://web.archive.org/web/20250113214946/https://www.geeksforgeeks.org/jquery-bind-method/
Open In App

jQuery bind() Method

Last Updated : 22 Apr, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The jQuery bind() method is used to attach one or more event handlers for selected elements. This method specifies a function to run when an event occurs. 

Syntax

$(selector).bind(event, data, function);

Parameters

It accepts three parameters that are described below:

  • event: This is an event type that is passed to the selected elements.
  • data: This is the data that can be shown over the selected elements.
  • function: This is the function that is performed by the selected elements.

Note: This method has been deprecated.

Example 1: In this example, “click” events are attached to the given paragraph using bind() method. 

HTML
<!DOCTYPE html>
<html>

<head>
    <script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
    </script>

    <script>
        $(document).ready(function () {
            $(".paragraph").bind("click", function () {
                alert("Paragraph is Clicked");
            });
        });
    </script>

    <style>
        .paragraph {
            width: 190px;
            margin: auto;
            padding: 20px 40px;
            border: 1px solid black;
        }
    </style>
</head>

<body>
    <p class="paragraph">Welcome to GeeksforGeeks</p>
</body>

</html>

Output: 

jQuery-bind-method

Example 2: In this example, the “click” event is added but this time data is also passed to the bind() method. 

HTML
<!DOCTYPE html>
<html>

<head>
    <script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
    </script>

    <script>
        function handlerName(e) {
            alert(e.data.msg);
        }

        $(document).ready(function () {
            $(".paragraph").bind("click", {
                msg: "Paragraph is Clicked."
            }, handlerName)
        });
    </script>

    <style>
        .paragraph {
            width: 190px;
            margin: auto;
            padding: 20px 40px;
            border: 1px solid black;
        }
    </style>
</head>

<body>
    <p class="paragraph">Welcome to GeeksforGeeks</p>
</body>

</html>

Output:

jQuery-bind-method


Become a Full-Stack Developer and also get 90% fee refund on completing 90% course in 90 days! Take the Three 90 Challenge today.

After successfully processing refunds worth over INR 5 Cr, GeeksforGeeks is back with the Three 90 challenge and this is your chance to upskill and get 90% refund. What more motivation do you need? Start the challenge right away!


Similar Reads

three90RightbarBannerImg