views:

90

answers:

4

I'm building somewhat of a different website, below is my HTML markup and my question. Please don't be put off by this wall of text, I'm sure it's really not a difficult problem for someone who know's their stuff but it takes some explaining.

<div id="0" class="cell" style="top: 0px; left: 0px;">
    <table border="0" cellpadding="0" cellspacing="0" width="100%">
        <tbody>
            <tr id="0">
                <td id="0">&nbsp;</td>
                <td id="1">&nbsp;</td>
                <td id="2">&nbsp;</td>
                <td id="3">&nbsp;</td>
                <td id="4">&nbsp;</td>
                <td id="5">&nbsp;</td>
                <td id="6">&nbsp;</td>
                <td id="7">&nbsp;</td>
                <td id="8">&nbsp;</td>
                <td id="9">&nbsp;</td>
                <td id="10">&nbsp;</td>
                <td id="11">&nbsp;</td>
                <td id="12">&nbsp;</td>
                <td id="13">&nbsp;</td>
                <td id="14">&nbsp;</td>
                <td id="15">&nbsp;</td>
            </tr>
            <tr id="1">
                <td id="0">&nbsp;</td>
                <td id="1">&nbsp;</td>
                <td id="2">&nbsp;</td>
                <td id="3">&nbsp;</td>
                <td id="4">&nbsp;</td>
                <td id="5">&nbsp;</td>
                <td id="6">&nbsp;</td>
                <td id="7">&nbsp;</td>
                <td id="8">&nbsp;</td>
                <td id="9">&nbsp;</td>
                <td id="10">&nbsp;</td>
                <td id="11">&nbsp;</td>
                <td id="12">&nbsp;</td>
                <td id="13">&nbsp;</td>
                <td id="14">&nbsp;</td>
                <td id="15">&nbsp;</td>
            </tr>
        </tbody>
    </table>
</div>

This markup is repeated in a tiling sort of pastern in order to fill the entire page. A similar DIV might be:

<div id="1" class="cell" style="top: 144px; left: 0px;">
    <!-- The rest of the table code here... -->
</div>

If you can't see it already, I'm creating a load of cells across the entire page sorted into DIVs. Now, when a user clicks into a cell (one of the <td>'s), I want to get it's co-ordinates represented by: 0, 1, 5.

In this example, 0, 1, 5 is the DIV with id of 0, the TR element inside that DIV with the ID of 1, and lastly the cell inside that TR element with an ID of 5. I wanted to write a javascript function to get these co-ordinates, but I am at a complete loss on what parameters to pass, and little idea how I can get out the co-ordinates.

From as far as I can think once I can pass a click event(?) to the function I can look at the <td>'s parent elements and get their IDs?

If anyone can provide a solution to this problem or provide any input, it'd be greatly appreciated.

Thank you. :)

A: 

Use the closest jQuery function, which returns the parent element that you specify.

Like this:

$("td").click(function () {
   var parentDIVId = $(this).closest("div").attr("id");
   var parentTRId = $(this).closest("tr").attr("id");
   var myId = $(this).attr("id");
}
RPM1984
yeah! great minds.. just that you don't need jquery to get the id of the `this`, simply do `this.id`... much better.. ;) and you miss one `)` at the end.
Reigel
true. just kept it like that for consistency to OP
RPM1984
This works perfectly, minus a small syntax error which I fixed myself :D Thank you so much!
VIVA LA NWO
pleasure. =) You should address the issues that Gaby has mentioned below though..
RPM1984
A: 

try

$('td').click(function(){
   var td = this.id;
   var tr = $(this).closest('tr').attr('id');
   var div = $(this).closest('div').attr('id');
});

you can test it here.

more on .closest() here

Reigel
lolz, great minds. i beat you to the punch though!
RPM1984
A: 

the easy thing would be

$('td').click(function(){
 $this = $(this);
 alert( $this.closest('div').attr('id') + ',' + $this.closest('tr').attr('id') + ',' + $this.attr('id'));
});

But you have some issues..

  1. you cannot have numbers as IDs
  2. IDs should be unique inside the DOM ...
Gaby
I'm not worried about valid markup, so I don't mind having duplicate ID's within the DOM :)
VIVA LA NWO
@VIVA LA NWO If you don't care about writing accurate code, why do you post here?
Doug Neiner
+1  A: 

Since it is not valid to begin an ID with a number, I'll offer a different solution.

Since your IDs are basically index numbers, you can use jQuery's .index() method to get what you need.

Test it here: http://jsfiddle.net/hBarW/

$('td').click(function(){
    var $th = $(this);
    var td_idx = $th.index();
    var tr_idx = $th.closest('tr').index();
    var div_idx = $(this).closest('div').index();
    alert(td_idx + ' ' + tr_idx + ' ' + div_idx);
});
patrick dw
Great answer Patrick, +1. I also found out there is a `cellIndex` and `rowIndex`: `var td_idx = this.cellIndex, tr_idx = this.parentNode.rowIndex`
Doug Neiner
@Doug - Wow, that's great! I didn't know that. Thanks for the info. :o) Here's an update using those properties: http://jsfiddle.net/hBarW/2/
patrick dw
@patrick, sweet! When I found out about it I was like "AWESOME!" :)
Doug Neiner