PaulEx10 PaulEx10 - 1 year ago 75
jQuery Question

Find hidden val from div (amongst multiple divs)

I've spent the past hour trying to figure this out and I can't seem to get it right. I've only just started learning jQuery but I feel I'm getting close to a solution for this problem of mine.

I have two divs.


<div class="fl person">
<input type="hidden" name="userSaved" id="userSaved" value="1" />
<img src="..." class="circle-mask" />

<div class="fl person">
<input type="hidden" name="userSaved" id="userSaved" value="2" />
<img src="..." class="circle-mask" />

I want to be able to get the hidden value (there's a form outside of all of this, I just didn't include it as it would just take up more room) from which ever div/image is clicked. When an image is clicked it hides that div. My jQuery for the value is quite off. I've had it so it gets the value but only from the first div, I can't seem to get it so that it displays the value of the hidden input from which ever div is clicked.


$("div.person img").click(function () {
$(this).hide(500, function () {
var $hidden = $('input#userSaved');
$(".main_page").appendTo("div.main_page").addClass("fl person");

Also, how do I access $hidden outside of the function?

Answer Source

Use the siblings() function:

var $hidden = $(this).siblings('input');

prev() would also work:

var $hidden = $(this).prev();

Side note: Your code has multiple elements with the same id, which is invalid. You should change that.

Edited to simplify the changed part.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download