Rod
Rod

Reputation: 15477

find the hidden field

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

Upvotes: 0

Views: 371

Answers (3)

Thomas Eyde
Thomas Eyde

Reputation: 3954

Give the hidden field an id:

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

And get the value directly:

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

Upvotes: 2

Soufiane Hassou
Soufiane Hassou

Reputation: 17750

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.

Upvotes: 0

user215361
user215361

Reputation:

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

Upvotes: 0

Related Questions