Tsuna Tsuna - 1 year ago 74
CSS Question

Couldn't find my span tag using jquery

I thought using closest goes up the DOM and find goes down the DOM isn't it?

I am trying to find a span element when an input has focusout. I am able to get the value of the input when focusout properly but couldn't find the span.

in my html I have something like this

<div class="form-group">
<label for="card-holder-name">Card Holder Name: <span>eafsdafasdf</span></label>
<input type="text" class="form-control card-holder-name" id="card-holder-name">

in jquery

$(".card-holder-name").on('focusout', function(){
var inputValue = $(this).val();


I tried using
to just find the label but no luck.

What am I doing wrong here?

Thanks in advance

Answer Source

.closest() does go up the DOM but you're trying to use it against a sibling.

Use .prev() in this case:


jsFiddle example

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download