javascript - get id from html table when clicking an input inside the table -


how can id of table when click input element?
don't need rowid etc.
i've tried parentnode.id can't seem id.
in end i'd this.

var tabelid = input....parentnode.parentnode.id; var table = document.getelementbyid(tabelid); 

example: enter image description here

how this:-

demo

html

<table id="tbltest">     <tr>         <td>             <input type="text" id="txttest" onclick="getparent.call(this)" />         </td>     </tr> </table> 

i using call here elements context inside getparent event callback.

javascript

function getparent() {   var parent = this.parentnode;   var tagname = "table";    while (parent) { //loop through until find desired parent tag name   if (parent.tagname && parent .tagname.tolowercase() == tagname) {       alert(parent .id);       return;     }       else       {           parent = parent .parentnode;       }    }  } 

if using jquery:-

in click event can $(this).closest('table').attr('id')


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -