0

I have the body of a table which is appended dynamically with ajax to the table element through a partial view in ASP.NET MVC. The partial view looks something like this...

@model IEnumerable<Plan>
@{
    Layout = null;    
}
@foreach (var plan in Model)
{
    <tr>
        <td>
            <a href="#" class="jumpToMyPlans">@plan.Name</a>
        </td>
    </tr>
}

...and appended to the table element which is static in the main View...

<table id="myPlanTable">
</table>

I am trying to register an onclick event for each of these anchor elements (there are a lot currently), using the jQuery on() function like this...

jQuery('#myPlanTable').on('click', 'tbody > tr:gt(0) > td > a.jumpToMyPlans', function () {
    console.log('click');
});

...but the event refuses to fire. I've checked the DOM traversal in the browser console and it's definitely correct and returns the expected DOM set.

4 Answers4

0

This selection statement worked for me....

jQuery('#myPlanTable').on('click', 'tr:gt(0) > td > a.jumpToMyPlans', function () {
        console.log('click');
});

I think the problem was specifying tbody in the selector, which I guess is probably also dynamically generated, though not by me, I guess by the browser or something.

0

When partial view is render through ajax or jquery then to bind Jquery events use "live" as following:

 jQuery('#myPlanTable').live('click', 'tbody > tr:gt(0) > td > a.jumpToMyPlans', function () {

        console.log('click');
    });
Suresh Kamrushi
  • 15,627
  • 13
  • 75
  • 90
RKT
  • 114
  • 5
-1

Try this:

jQuery('#myPlanTable').on('click', 'tbody > tr:gt(0) > td > a.jumpToMyPlans', function () {
     console.log('click');
});

1) Change a.jumpToMyActionPlans to a.jumpToMyPlans

2) Add ); to close your click handler

Felix
  • 37,892
  • 8
  • 43
  • 55
-1

I tested this and the click worked:

$(document).on('click', '#myPlanTable a.jumpToMyPlans' , function () {
    alert('click');
});

the html i used to test it was:

<table id="myPlanTable">
     <tr>
        <td>
            <a href="#" class="jumpToMyPlans">PAPA</a>
        </td>
    </tr>
    <tr>
        <td>
            <a href="#" class="jumpToMyPlans">Sapo</a>
        </td>
    </tr>
</table>

Hope this helps you!

alecellis1985
  • 131
  • 2
  • 15
  • Hm that's odd, specifying 'tbody' for me makes mine not work, but works without it –  Feb 10 '14 at 17:49
  • 1
    Sorry, there is no tbody in your table, it would be #myPlanTable the first selector. going to fix in the answer. Im glad it worked :) – alecellis1985 Feb 10 '14 at 17:54
  • Yeah I'm using chrome to develop and apparently it adds tbody, theader and tfooter to table elements. I was looking at the generated DOM, seeing tbody and assuming that, since i hadn't created it, it would be reference-able when actions are delegated. Clearly wrong haha, best to leave it out entirely –  Feb 10 '14 at 18:11
  • Incidentally, for that reason I don't think this a a duplicate of any other question, I wasn't using syntax wrong it was a browser idiosyncracy that other people might want to be aware of. Not sure why people voted this as duplicate. –  Feb 10 '14 at 18:12