Miguel Moura
Miguel Moura

Reputation: 39384

Get first element with class X after

I have the following markup example:

<div class="wrap">
  <input name="country"/>
  <span class="message">Fill your country</span>
  <span class="error">Error</span>
</div>

The error span can be immediately after the input or not.

Being element then country input I have to get the error span:

var error = element.parent().children("span.error");

This is working but does not feel right ... Is there a better way?

Basically, I need to get the first span.error after 'input.country'.

Upvotes: 0

Views: 35

Answers (2)

Akshey Bhat
Akshey Bhat

Reputation: 8545

var error = element.siblings("span.error:first");

Use siblings method.

Upvotes: 0

Venkata Krishna
Venkata Krishna

Reputation: 15112

To get the first error span

element.siblings("span.error:first")

To get all error spans

element.siblings("span.error")

Upvotes: 1

Related Questions