1

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?

1
  • what do you do in those click functions? is it the same thing or different Commented May 10, 2013 at 8:00

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

You need to post the seven functions in your code then or may be the first three only. Then only we can find some solution...
@prasanth If you are using seven functions then you could write functions for each. But if you are adding <a> tags dynamically then you should follow as palash suggested. Selecting all ids are not recommended and there isn't any need to do so
0

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*/
    ------
});

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.