Reputation: 24019
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
Reputation: 93621
The parent label
s 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