javascript - Get td parent and tr child -


i need value "test1" first td clicking on button. tried javascript doesn't work..the print gave me undefined.

   <table style="width:100%">         <tr>             <td>test1</td>             <td>test2</td>             <td><button class='x' type='button' onclick='openindex()' >value='button'</button></td>         </tr>        </table> 

here javascript function, can't figure out i'm doing wrong because find closest tr child property doesn't work

function openindex(){     var $row = $(this).closest("tr");         $tds = $row.find("td:nth-child(1)").value;  alert($tds); } 

you calling openindex() without explicitly context, this (inside it) window , not element.

you pass it:

onclick="openindex.call(this)" 

but should avoid using onclick attributes in first place , bind functions jquery().on instead.

jquery('button').on('click', openindex); 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -