prasanth
prasanth

Reputation: 115

jquery multiple click event in one click event

i used this below function...for each id i call one function..i call only one click function at a time so i need to use single click function for this..

      .append($('<a>',{'class':'list-header','id':'call1','name':'name','value':'1'}).append('1'))
      .append($('<a>',{'class':'list-header','id':'call2','name':'name','value':'2}).append('2'))
      ...
      ...
      ...
      .append($('<a>',{'class':'list-header','id':'call7','name':'name','value':'7'}).append('7'))));


      $('#call1').click(function(){

      });
      $('#call2').click(function(){

      });
      ...
      ...
      ...
      $('#call7').click(function(){

      });

i have use seven function above..i will call only one function at a time. so i need to do it in a single function..

how to do it?

Upvotes: 1

Views: 135

Answers (3)

Subin Jacob
Subin Jacob

Reputation: 4864

Attach click event for all objects by class selection.

$(document).ready(function() {
    $(".list-header").click(function(clkEvt) {
        var ClickedAtag = $(clkEvt.target);

        alert(ClickedAtag.id);
    });
});

ClickedAtag is the element that clicked by the user. You can use this object to do any unique function for the clicked element.

Upvotes: 0

Suresh Atta
Suresh Atta

Reputation: 121998

you can try

$('#call1, #call2, #call3, #call4,#call5,#call6,#call7').
                                                   click(function(event){ 
    if($(event.target).attr('id')=='call1'){
        /* specific code for call1*/
    } else if($(event.target).attr('id')=='call2'){
        /* specific code for call2*/
    ------
});

Upvotes: 0

palaѕн
palaѕн

Reputation: 73906

You can simply use the class for that, which you already have :

$(document).on('click', '.list-header', function(){
    alert(this.id);
    // Your code goes here
});

Also, you need to use on method here, since the links are dynamically added here.

Upvotes: 2

Related Questions