tags:

views:

193

answers:

3

I need to get neighborhood element value.

HTML is

<div>
    <input type='hidden' value='12345'>
    <div id='click-this'>Click me</div>
</div>

How can i get "12345" by clicking "click-this" div ?

$('#click-this').click(function() {
    ??? 12345 ???
})
+1  A: 

You could do this in multiple ways, but the word neighborhood suggests you could use siblings:

$('#click-this').siblings('input').val();
John McCollum
A: 

I haven't tested this but try: (based on documentation of jQuery 1.4.2)

$('#click-this').click(function() {
   alert($(this).prev().val());
});
Roberto Sebestyen
A: 

Few more ways :-

$('#click-this').click(function() {
    var value = $(this).parent().children().eq(0).attr('value'); 
                      or
    var value = $(this).parent().children().eq(0).val(); 
});
Pawan Mishra