Bronzato
Bronzato

Reputation: 9332

jQuery select my input tag above

When a user click on my icon-calendar inside the i tag I would like to reference the input just above.

<div class="input-control text">
    <input type="text" data-bind="text: fromDate" />
    <i data-bind="datepicker: fromDate" class="icon-calendar btn"></i>
</div>

So how to use jQuery to select the input above my i tag.

Something like:

$(element).closest('input')

Where element is my i tag clicked.

Thanks.

Upvotes: 0

Views: 106

Answers (2)

Eli
Eli

Reputation: 14827

closest() will traversing up through its ancestors in the DOM tree not the siblings of your element, you can use siblings() or prev():

$('.btn').siblings('input').data('bind')
$('.btn').prev().data('bind')

Fiddle

Upvotes: 0

Yury Tarabanko
Yury Tarabanko

Reputation: 45121

http://api.jquery.com/prev/

$(element).prev("input").first();

Upvotes: 1

Related Questions