Friday, February 8, 2013

Bind Multiple Controls to a Single Event in jQuery

In this sample, we will see how to bind Multiple Controls to a single event in jQuery. We will bind 5 button controls (Button A, B, C, D and E) to a single click event. This example also demonstrates how to create an array of controls and loop through them. Here’s the sample:


<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Bind Multiple Elements to Single Event</title>
        <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
        <script type="text/javascript">
            $(function () {
                var btns = $('#buttonA,#buttonB,#buttonC,#buttonD,#buttonE');
                $.each(btns, function () {
                    $(this).click(function () {
                        alert(this.id + " is clicked.");
                    });
                });
            });
        </script>
    </head>
    <body>
        <input id="buttonA" type="button" value="Button A" />
        <input id="buttonB" type="button" value="Button B" />
        <input id="buttonC" type="button" value="Button C" />
        <input id="buttonD" type="button" value="Button D" />
        <input id="buttonE" type="button" value="Button E" />
    </body>
</html>

Happy reading !! :)
kick it on DotNetKicks.com

No comments:

Post a Comment

^ Scroll to Top