StudioTime
StudioTime

Reputation: 24019

Traversing to next input element - JQuery

I have the following HTML structure:

<label>
  <input id="Email">
  <p>...</p>
</label>
<label>
  <input id="randomID">
</label>

When a certain thing happens I want the next input AFTER the input with id=Email to gain focus()

Here's what I'm trying but it fails, meaning, focus not given:

$('#Email').nextAll('input').first().focus();

I won't always know the ID of the next input otherwise I know it would be easy

Upvotes: 1

Views: 106

Answers (1)

iCollect.it Ltd
iCollect.it Ltd

Reputation: 93621

The parent labels add another branch, causing nextAll to not find it. nextAll() only looks at sibling elements under the same parent.

try this:

$('#Email').closest('label').next().find('input').first().focus();

As focus() will only target the first element in a set anyway, the first() is not required:

$('#Email').closest('label').next().find('input').focus();

Upvotes: 1

Related Questions