Ben
Ben

Reputation: 9001

Shorthand jQuery to focus first input in form in div

What I'm trying to do

On the user click, I wish to focus on the first input in the (only) form inside the child div with the class detailEdit.

The jQuery code I have works - but it seems like a fairly long way round. Is there a shorthand version?

My Code

HTML/PHP

<div class="detailEdit">
    <form id="frm_contact">
        <span>Office:</span><br/>
        <input type="text" placeholder="Company Telephone" id="frm_tel2" name="tel2" maxlength="11" value="<?php echo $userData['tel1']; ?>" />
        <span>Mobile:</span><br/>
        <input type="text" placeholder="Mobile Number" id="frm_tel1" name="tel1" maxlength="11" value="<?php echo $userData['tel2']; ?>" />                         
        <input type="submit" value="Submit" name="submit" />
    </form>
</div>

jQuery

   $(this).children('div.detailEdit').first().children('form').first().children('input').first().focus();

Upvotes: 4

Views: 8593

Answers (3)

Milan and Friends
Milan and Friends

Reputation: 5610

You have input id, why not use it

$('#frm_tel2').focus();

you can also use

$('.detailEdit input:first').focus();

Upvotes: 1

Alessandro Minoccheri
Alessandro Minoccheri

Reputation: 35973

try this:

$('#frm_contact').find('input:first').focus();

or this if you don't want to use id of the form:

$('.detailEdit').find('form').find('input:first').focus();

Upvotes: 0

Nikola Loncar
Nikola Loncar

Reputation: 2671

Try something like this :

$('.detailEdit').find('input:first').focus();

Upvotes: 10

Related Questions