开发者

jQuery - get neighborhood element

开发者 https://www.devze.com 2022-12-25 13:34 出处:网络
I need to get neighborhood element value. HTML is <div> <input type=\'hidden\' value=\'12345\'>

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(); 
});
0

精彩评论

暂无评论...
验证码 换一张
取 消