show a field and set focus to that field

I have several input text fields that are hidden.  When a button is clicked it calls:

function showlines(index) {
            $( "div.attwrapper" + index).toggleClass( "show-field", 500, "linear"  );
            document.getElementById( "div.attwrapper" + index).focus();
            break;
}


The document.getElementById was my attempt to set focus on the text field within the div.attwrapper .
It doesn't work, anyone know how I can set the focus on the text field?
Gary SamuelsPlant ManagerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chaauCommented:
You need to use getElementsByTagName to get an array of elements of type "input" within your div. Then just pick the first one and set the focus:
    var div = document.getElementById( "div.attwrapper" + index);
    var txtBoxes = div.getElementsByTagName('input');
    if(txtBoxes.length) > 0
        txtBoxes[0].focus();

Open in new window

If you have more than one "input" elements in the div you may need to check their types to make sure they are not checkboxes or buttons:
    var div = document.getElementById( "div.attwrapper" + index);
    var txtBoxes = div.getElementsByTagName('input');
    for(var i = 0; i < txtBoxes.length; i++) 
    {
        if (chk[i].type == 'text') 
        {
            txtBoxes[0].focus();
        }
    }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Chris StanyonWebDevCommented:
You're trying to get an element by id, by passing in a class! It won't work.

Take a look at this:

function showlines(index) {
    var div = $('div.attwrapper'+index);
    div.toggleClass("show-field", 500, "linear");
    $('input[type=text]', div).focus()
}

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.