Jquery, get columname when cell clicked -


how can column name jquery when cell clicked?

you can obtaining index of cell, , getting text header same index.

i have uploaded demo here:

http://jsfiddle.net/sohnee/dnxtz/23/

the jquery looks this:

$("td").click(function(){     var $this = $(this);     var col = $this.parent().children().index($(this));     var title = $this.closest("table").find("th").eq(col).text();     alert(title); }); 

and relies on proper table structure....

<table>     <thead>         <tr>             <th>name</th>             <th>address</th>         </tr>     </thead>     <tbody>         <tr>             <td>steve</td>             <td>uk somewhere</td>         </tr>         <tr>             <td>scott</td>             <td>usa somewhere</td>         </tr>     </tbody> </table> 

note: caption, tfoot et al, omitted optional stuff can added , script still works.

http://www.w3.org/tr/html401/struct/tables.html


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -