0

Here is my problem. I have nested tables something like this

<table id="tblMenu">
                    <tr>
                        <td valign="top">
                            <table id="tblMenu1">
                                <tr>
                                    <td>
                                        <div class="submenuRed" onclick="emptyTd();">                                                
                                        </div>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <div class="submenuRed" onclick="emptyTd();">                                               
                                        </div>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <div class="submenuRed" onclick="emptyTd();">                                               
                                        </div>
                                    </td>
                                </tr>
                            </table>
                        </td>
                        <td valign="top">
                        </td>
                        <td valign="top">
                        </td>
                        <td valign="top">
                        </td>
                    </tr>
                </table>

When I click on div nested deep inside I want to find out the column position of table tblMenu.

How can I get it using jQuery

Pls suggest!

Thanks! Arshya

Arshya
  • 660
  • 3
  • 12
  • 30

2 Answers2

1

.closest() and .index()

function emptyTd() {
    var idx = $('#tblMenu1').closest('td').index();
    // index() starts counting at zero
};

http://jsfiddle.net/mblase75/KGEtq/

Blazemonger
  • 90,923
  • 26
  • 142
  • 180
  • I want to find out the position of column in table tblMenu not tblMenu1. See there are 4 td in tblMenu and the table tblMenu1 moves between these columns. I want to know which column in table tblmenu its in currently – Arshya May 29 '13 at 16:11
  • [Are you sure?](http://jsfiddle.net/mblase75/KGEtq/) Your question is unclear. Is there *more* HTML you didn't post in your question? – Blazemonger May 29 '13 at 16:16
  • If you want the column *containing* `tblMenu`, just change the selector in my code to `$('#tblMenu')` and you should be good. – Blazemonger May 29 '13 at 16:18
1

add $(this) in the call of the emptyId function

and do this :

function emptyId(div) {
    div.text($('td[valign="top"]').index(div.closest('td[valign="top"]')) + 1);
}

it should work

mlwacosmos
  • 4,391
  • 16
  • 66
  • 114