Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello friends,
I trying to create dynamically footer and append to the table in MVC using following script
function createFooter(count) {
        var rowsPerPage = 5;
        var footer = "<tfoot><tr><td>";
        for (var i = 1; i < count + 1; i++) {
            footer = footer + "<a class='Paging' href=#>" + i + "</a> ";
        }
        footer = footer + "</td></tr></tfoot>";
        $('#DataTable thead').after(footer);
        return footer;
    }


I'm trying to click the tag, but its not working
I have used following script
 $('#DataTable tfoot a').click(function (e) {
            alert('hi');
            e.preventDefault();
)};


Any idea why it is not getting fired??
Thanks in advance
Posted

1 solution

Hi,

For jQuery 1.7+, use on[^] instead of click:
JavaScript
$('#DataTable tfoot').on('click', 'a', function(e) {
       alert('hi');
       e.preventDefault();
});

For older versions than 1.7, use delegate[^]:
JavaScript
$('#DataTable tfoot').delegate('a', 'click', function(e) {
       alert('hi');
       e.preventDefault();
});

Hope this helps.
 
Share this answer
 
v3
Comments
dhage.prashant01 26-Aug-13 8:29am    
.live() is working fine
why i need to use live() and what was problem with .click() in version jquery-1.4.4.js??
if possible please provide comments, that will help me to learn more..

Thanks
Thomas Daniels 26-Aug-13 8:30am    
click simply doesn't work on dynamically generated elements. By the way, I updated my answer. If you use jQuery 1.4.3+, then use delegate instead of live, but if you use jQuery 1.7+, use on.
dhage.prashant01 27-Aug-13 3:00am    
I have changes the Jquery version to jquery-1.7.2.js
I tried to use .delegate as well .on as per your solution, but they are not working.
But when i tried live, it is working fine.

why .delegate and .on not working??
Thomas Daniels 27-Aug-13 3:02am    
Have you tried this:

$('#DataTable tfoot').on('click', 'a', function(e) {
alert('hi');
e.preventDefault();
});
dhage.prashant01 27-Aug-13 3:11am    
Your solution did not work, but I modified the same as bellow

$(document).on('click', '#DataTable tfoot a', function (e) {
alert('hi');
e.preventDefault();
}

and it is working fine.

.live() too was working fine, then why use of .live() is not recommended??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900