rosuandreimihai
rosuandreimihai

Reputation: 656

jQuery find input value from parent siblings

I have a piece of html code like the one below:

<div>
    <div><input id='route' value='1'></div>
    <div>
        <select id='selID'>
            ...
        </select>
    </div>
</div>

Now, I want to get the value from the input once I select any option from select element I have tried something like this, but still with no succes:

$(document).on('change', '#selID', function (event) {
    alert($(this).parent().siblings('div').children('input').val());    
});

Could someone give me a solution? Thank you in advance!

Upvotes: 0

Views: 3292

Answers (2)

Sam
Sam

Reputation: 4484

You can do it like this -

$("#selID").on('change', function(){

    var $a = $(this);
    if($a.val() != ""){
        alert($a.parent().parent().find('input').val());
    }

    return false;
});

Fiddle: http://jsfiddle.net/vzmzu6zc/

Upvotes: 2

Lumi Lu
Lumi Lu

Reputation: 3305

try to use $('#selID') instead of $('document')

$('#selID').on('change', function (event) {
    alert($(this).parent().siblings('div').children('input').val());    
});

FIDDLE

Upvotes: 0

Related Questions