-2

A part of my table looks somewhat like this:

<td data-item="Tier1">
     <a class="infolnk" href="#">DRAFT</a>
</td>
<td data-item="Tier2">
     <a class="deletelnk" href="#"></a>
     <a class="editlnk" href="#"></a>
     <a class="infolnk" href="#">DRAFT</a>
</td>
<td data-item="Tier3">
     <a class="deletelnk" href="#"></a>
     <a class="editlnk" href="#"></a>
     <a class="infolnk" href="#">DRAFT</a>
</td>

To handle the on-click -events, i've written the following function:

$(document).ready(function () {
     Console.log("loading Tiers");
     LoadTiers();
     Console.log("Tiers have been loaded, loading Eventhandlers");
     SetTableEventHandlers();
}


function SetTableEventHandlers() {

    $(".deletelnk").click(function ()
    { _delete($(this)); });

    $(".infolnk").click(function ()
    { _getInfo($(this)); });
}

function _delete(lnkClicked)
{
   alert("delete clicked");
   ....
}

function _getInfo(lnkClicked)
{
   alert("Info clicked");
   ....
}

Yet only the delete seems to be working. I'm not getting any alert from _getInfo. Can anyone see why?

Note: the tags with class="deletelnk" are declared on the html-page, class="infolnk" are dynamically added. Yet the setting of the event handlers occurs after they have been added dynamically.

User999999
  • 2,500
  • 7
  • 37
  • 63
  • Can you show where you trigger the event where you set the event handlers? – putvande Jul 30 '14 at 13:05
  • possible duplicate of [Event binding on dynamically created elements?](http://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements) – emerson.marini Jul 30 '14 at 13:06
  • "Yet the setting of the event handlers occurs after they have been added dynamically." Are you 100% sure that the elements are loaded before the event handlers are setup? – Richard Dalton Jul 30 '14 at 13:08
  • @RichardDalton. Yes following the logging the elemens are first added and then the eventhandlers are added. – User999999 Jul 30 '14 at 13:11

3 Answers3

1

event binding for dynamically added element can be done using .on, so change

$(".infolnk").click(function ()
    { _getInfo($(this)); });

to

 $("table tr").on("click",".infolnk",function ()
    { _getInfo($(this)); });
Bhushan Kawadkar
  • 28,279
  • 5
  • 35
  • 57
  • Aha! Will try your option in an instant – User999999 Jul 30 '14 at 13:15
  • as dynamically created elements are not available after `document.ready` and hence cannot bind events using `.click`. So jQuery provided `.on('click','selector',function(){...})` for binding events. For more information see [this](http://stackoverflow.com/questions/9122078/difference-between-onclick-vs-click) – Bhushan Kawadkar Jul 30 '14 at 13:19
  • Apparently your right. I always assumend (obviously wrongly) that `click` worked in the same way as `on` once the elements were loaded into the `DOM`. Thanks for pointing this out! – User999999 Jul 30 '14 at 13:22
0

You are missing closing a tags

<a class="deletelnk" href="#"></a>
<a class="editlnk" href="#"></a>
<a class="infolnk" href="#">DRAFT</a>
Aleksandar Gajic
  • 1,349
  • 1
  • 18
  • 23
-1

You can use

$(".deletelnk").click(function (){
    _delete($(this)); 
});

$(".infolnk").click(function (){
    _getInfo($(this)); 
});

function _delete(lnkClicked){
    alert("delete clicked");
}

function _getInfo(lnkClicked){
    alert("Info clicked");
}
Mijoe
  • 236
  • 2
  • 9