views:

85

answers:

3

Hi All,

I have a table with an image on each row with a hover event attached. Can someone please show me how to alert the value of the hidden field?

<div class='first'>
    <input type="hidden" value="target">
    <div class='second'>
     <img src="yada" />
    </div>
</div>

thanks, rodchar

A: 

You can find the image tag object in your event handler, then get the parentNode and traverse the DOM tree to get the value.

Matthias Vance
A: 

For this specific case, you could do :

$("img[src='yada']").parent().prev().attr('value');

To get the value.

But, it's not a good practice. This code will easily break when you change your HTML structure.

Soufiane Hassou
You could use .prev("input[type='hidden']") instead of parent().prev()
Terw
+2  A: 

Give the hidden field an id:

<input id="hidden_yada" type="hidden" value="target">

And get the value directly:

$("#hidden_yada").attr('value');
Thomas Eyde