Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
when dynamically binding table
Getting td value as undefined when td click function

static table function is working fine.Not working on dynamic binding

What I have tried:

<pre>  var sttt = "  <tr class='parent' id='other_app'> <td align='center' > <input class='updateBtn' type='button' name='btn_id' value='Update' onclick='test()'> </td>" +
        "<td name='App_Name'>Test1</td><td align='center'>None</td><td align='center'>Desktop</td> <td align='center'>2014-06-30 18:22:39</td>" +
        " </tr > <tr class='parent' id='other_app'> <td align='center'> <input class='updateBtn' type='button' name='btn_id' value='Update' onclick='test()'>" +
        " </td> <td name='App_Name'>Test1</td> <td align='center'>None</td>  <td align='center'>Server</td><td align='center'>2014-03-30 16:20:15</td></tr>";
   
    $("#tst").append(sttt);


function test()
   {
       var name = $(this).parent().parent().find('td').eq(1).html()
       var time = $(this).parent().parent().find('td').eq(4).html()
       alert(name);
       alert(time);
   }
Posted
Updated 6-Feb-18 0:42am

1 solution

"this" will contain the window object, not the button inside td, you will have to pass the button object as the argument to the method and access its parent objects as
<input class='updateBtn' type='button' name='btn_id' value='Update' onclick='test(this)'
function test(btn)
   {
       var name = $(btn).parent().parent().find('td').eq(1).html()
       var time = $(btn).parent().parent().find('td').eq(4).html()
       alert(name);
       alert(time);
   }
 
Share this answer
 

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