Kobojunkie
Kobojunkie

Reputation: 6545

Jquery/JavaScript code binding to collection

I have a dynamically generated table of records and for each row, I have an anchor tag with class name set to 'hdelete' to enable me invoke delete method on the particular row's link that is clicked. I have some code currently that is support to hook up all the anchors with class ='hdelete'

  $("#tbl_srecords").click(function (e) {
       $(e.target).hasClass("hdelete") ? fnDeletehrecord($(e.target)) : null; //Run the delte row function here
    });  

The code above does not seem to work. what it currently does is select just the first occurence of anchor with class='hdelete'. Anyone with a better idea on how to best implement this?

Upvotes: 0

Views: 51

Answers (3)

Kevin Bowersox
Kevin Bowersox

Reputation: 94499

This will bind a function to all anchor tags with class hdelete and call the fnDeletehrecord function with the element as a jquery object past as a parameter.

   $("#tbl_srecords a.hdelete").click(function(){
       //NOTE: This represents the anchor tag that fired the event
       fnDeletehrecord($(this));
    });

Upvotes: 1

The Alpha
The Alpha

Reputation: 146229

I have a dynamically generated table

$("#tbl_srecords a.hdelete").live('click', function (e) {
    fnDeletehrecord($(this)); // pass the element as a parameter to the function
});

live is Deprecated

This is latest (recommended)

$("#tbl_srecords").on('click', 'a.hdelete', function (e) {
    fnDeletehrecord($(this)); // pass the element as a parameter to the function
});

because of dynamic content.

Upvotes: 2

dfsq
dfsq

Reputation: 193291

This should work:

$("#tbl_srecords a.hdelete").click(fnDeletehrecord);

This code will handle click events on the links with class hdelete within element with id tbl_srecords.

Upvotes: 0

Related Questions