Delegate Method Using JQuery

In this article, I have tried to explain use of Delegate method with jQuery.

The delegate () method use for handle a single event for different control such as if you have many control in your HTML page like (button, hyperlink, radio button, checkbox and etc) and you want to fire common event on the click of any control, then we will use the delegate () method and this delegate () method will work for both controls current as well as future (like a new element created by a script).

$(selector).delegate( childSelector, event, data, function)
jQuery Selectors

Through the jQuery Selectors we can select and frame up HTML element(s).it will help to find elements based on their id, classes, types, , attributes, values of attributes and much more. It is based on the existing CSS Selectors, and in addition, it has some own custom selectors. Any type of selectors in jQuery start with the dollar sign and parentheses: “$()”.The jQuery element selector selects elements based on their tag names.

$("input").on("click", function () {alert(“You Clicked on Input Tag”);}



Specify one or more child controls to attach the event handler

this parameter is necessary.



Specify one or more Events to attach the controls and multiple Events values by separating the space but events should be valid this parameter is necessary.



This parameter is optional it will give the additional data pass for the function.



Specifies the function to run when the event occurs this parameter is necessary.


I will take an example for illustrate the Delegate method in jQuery.

In this example I have many controls (input, hyperlink, comboBox and image) and if I click on any control will execute a single event function and will show an alert message. And you must have jquery file in your home directory.

Delegate Method Using JQuery

And this example will give the below output.

Delegate Method Using JQuery

My HTML Code:
<table border="1px">

                <td><input type="button" id="btnButton1" value="Button1" /></td>
                <td><input type="button" id="btnButton2" value="Button2" /></td>
                <td>a href="#">HyperLink1</a></td>
                <td> <a href="#">HyperLink2</a> </td>
                    <option value="up">U.P</option>
                    <option value="mp">M.P</option>
                    <option value="hp">H.P</option>
                    <option value="jk">J&K</option>
                    <option value="ald">Allahabad</option>
                    <option value="rewa">Rewa</option>
                    <option value="kanpur">Kanpur</option>
                    <option value="noida">Noida</option>
<td><img src="twitter.png" alt="twitter" width="42" height="42"></td>

 My jQuery Code:

<script src="jquery-1.7.1.js"></script>

<script type="text/javascript">
    $(document).ready(function () {
        $("input").on("click", function () {
            var message = "Click :" + $(this).val();
        $("a ").on("click", function () {
            var message = "Click :" + $(this).text() ;
        $("option").on("click", function () {
            var message = "Select :" + $(this).text();
         $("img").on("click", function () {


Last updated:9/7/2019 12:13:48 AM


Leave Comment