jQuery - get neighborhood element
I need to get neighborhood element value.
HTML is
<div>
<input type='hidden' value='12345'>
<div 开发者_JS百科id='click-this'>Click me</div>
</div>
How can i get "12345" by clicking "click-this" div ?
$('#click-this').click(function() {
/*
* Get siblings element's:
* at this context, input tag element with value 12345
*
*/
})
You could do this in multiple ways, but the word neighborhood suggests you could use siblings:
$('#click-this').siblings('input').val();
I haven't tested this but try: (based on documentation of jQuery 1.4.2)
$('#click-this').click(function() {
alert($(this).prev().val());
});
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();
});
精彩评论