Explanation
I think the others are making it too difficult for you.
The problem with your code is that you're attempting to find a td
within a td
.
When you do $(this)
within the context of an event function, it refers to the element whose event was triggered.
$('.divOne').hover(function(){
$(this); //<- "this" refers to the current "divOne" the user is hovering.
// ".divOne" or $(this) is a td
$(this).find("td.hidden"); //<- Attempts to find a td with the class "hidden" within ".divOne"
});
However, if you'd use the proper tree traversal function siblings
and the pseudo class :hidden
you'd be successful in your endeavor.
$('.divOne').hover(function(){
var id = $(this).siblings(":hidden").html();
});
That would give you all the td
s that are hidden. Since your example only had one column that was hidden, it should suffice.
If you'd want a specific sibling you could either add a class or use a combo :nth-child(n):hidden
.
$('.divOne').hover(function(){
var id = $(this).siblings(".myclass:hidden").html();
});
Example
The easiest way to grab the id of the third, and last, td
is by using either of the pseudo-classes
You could also define your own class, like myclass
and then do
$("td.myclass")
Here's a working JSFiddle example | Code
$('table tr').hover(
function(){
var id = $("td:nth-child(3)", this).text();
//var id = $("td:last-child", this).text();
//var id = $("td.myclass", this).text();
$("#Details").html("Details box.<br /> Hovering \""+id+"\"");
$("#Details").show();
}, function() {
$('#Details').hide();
}
);
Notice the use of the selector, where I instead use table tr
so that the events only need to be triggered once you hover in and out a table row. This also enables me to use a context reference. Now $(this)
refers to the row the user is hovering, and all its children (td
s) are within it.
I also use a selector with a second parameter, defining the context of my selection. By default it is the entire document.
This limits the selection to the element the user is hovering and its children. Err... by that, I mean not the users children, but the element's children.
$("td:nth-child(3)", this)
equals to: find the third table division within this row I'm hovering
References