0

I had write a simple code to delete a record on a table,

<td><a href = "#" class = "delete">Delete</a></td>

$('.delete').on('click', function() { 
     console.log('try');
});

The problem is that, If how many td's on the document I mean if there are 10 td's with delete class, it logs the try strign 10 times also.

Am I using on click event listener wrong or this is an issue on the jquery. Thank you.

9
  • 1
    Is that how its working or how you expect it to work? The eventhandler should only be fired once for each element clicked (that has the class). Don't assume, just test! Commented Oct 22, 2013 at 9:35
  • 3
    do you perhaps have your .on() call inside the loop that creates each <td> ? Commented Oct 22, 2013 at 9:37
  • I don't see anything wrong with your code Commented Oct 22, 2013 at 9:37
  • there is nothing wrong there jsfiddle.net/arunpjohny/37H7B/1 Commented Oct 22, 2013 at 9:38
  • Nope, If the user clicks only a delete link, then it will only log once. Not looping through all delete class and logs it ten times Commented Oct 22, 2013 at 9:38

1 Answer 1

3

A single call to .on() will only ever result in a single event handler being launched.

IMHO, the most likely explanation for your fault is that you are actually calling .on() multiple times - perhaps inside the loop that's creating the <td> elements?

Within the single handler you can use DOM traversal to identify which column or element was clicked, and act accordingly. If, for example, you wanted to delete the current row you could use this:

for (...) {
    // populate the table here
}

// handler will remove the row containing the clicked ".delete" link
$('.delete').on('click', function() {
    $(this).closest('tr').remove();
});

EDIT based on new information from the OP, the problem is actually duplicate re-registration of the same handler over and over, each time a row is added.

In this case, the best solution is event delegation, where you put the handler on the table (just once), rather than on the individual elements:

$('#mytable').on('click', '.delete', function() {
    // do your delete stuff here
});

Note the additional .delete parameter to .on - this tells jQuery which particular elements within the table you're interested in - the clicked element will be passed as this.

You can make this call before the table is even populated, and it'll work on any .delete element found inside the table, even ones added later, dynamically.

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

2 Comments

+1 this is most likely the case and the solution seems nice. If the table is populated above it might also be beneficial to create the click handler per element above (inside the for loop) rather than using a class name at all. If we use $.each or a .forEach loop (the latter in modern browsers) we can get pretty nice code here and create a scope for the elements and the backing data together which would allow us to separate concerns.
@Alnitak thank you, what information you had given is awesome. This solves the issue and the best way to do this kind of stuff.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.