I have a table with some values. The first row and first column contain values of such called vertical and horizontal sizes respectfully. What I need to do is to click on a table cell and get the horizontal and vertical values for that clicked table cell. What it basically means – get the value of first table cell in the row where the clicked table cell is located and get the first table cell value of the column where the clicked table cell is located. I’m getting the horizontal value with the following code:
var horizSize = $('th:first', $(this).parents('tr')).text();
But I’m not sure how to do that for the vertical line. How to get to the first table cell value of the column?
Thanks for helping.
I think the following should work:
JS Fiddle demo.
Note that in the demo I’ve also amended your
horizSizeselector, to:And subsequently refined to:
The above assumes that you want the value of the cell from the
:firsttrelement, whether that be in thetheadortbody. If you’ve marked up the heading cells asththen you’ll need to amend thetdof the selector toth.Edited in response to Felix Kling’s comment:
That’s something I should’ve thought of, myself, really. And, if there is more than one
tablein the document then this change would be essential, to demonstrate:JS Fiddle demo.
References:
closest().eq().find().:firstselector.index().:lastselector.prevAll().