Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 532583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:25:13+00:00 2026-05-13T09:25:13+00:00

I have a requirement in Javascript (using Prototype) to focus the cursor on the

  • 0

I have a requirement in Javascript (using Prototype) to focus the cursor on the first form item within a specific div.

Simple example:

<form id="theForm">
  <div id="part1">
    <input id="aaa" .../>
    <select id="bbb">...</select>
  </div>
  <div id="part2">
    <select id="ccc">...</select>
    <input id="ddd" .../>
  </div>
</form>

I want to write a Javascript function that takes the name of a div within theForm and focuses the cursor on the first item in that div – e.g.

focusOnFirst('part1'); // will put focus on input "aaa"
focusOnFirst('part2'); // will put focus on select "ccc"

I thought I might be able to use Prototype’s select method like this:

$(pDiv).select('input','select',...etc.);

However, this returns an array that contains all the inputs, followed by all the selects, etc. It doesn’t return the items in the order they appear within the div. In my example above, this results in putting focus on the input “ddd” rather than the select “ccc”.

Another possibility is the Form getElements method:

$('theForm').getElements();

But now I need to interrogate each item returned to see if it falls inside the required div. And currently the only way I know to do that is to get all its ancestors and see if any is the selected div. I could do that, but I fear it won’t be the most efficient solution.

Can anyone suggest a better way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:25:13+00:00Added an answer on May 13, 2026 at 9:25 am

    What about using a CSS3 selector:

    $$('#theForm input:first-of-type')[0].focus()
    

    It returns an array, even if we know it’s only going to contain one element, so just grab the first one and focus it.

    I’ve worked up a quick example.

    –Edit OK, I just realised it misses the select element.

    –Edit This seems to work – select the first element with a name attribute (assuming you’ve got name attributes, could use something else):

    function focusOnFirst2(divName) {
        $$('#' + divName + ' [name]')[0].focus();
    }
    

    I’ve updated my linked example to include this as well as my erroneous first attempt.

    –Edit One last time 🙂

    I’ve taken your function and attempted to turn it around so it searches based on the children of the div rather than the children of the form. I stuck it in a loop to call it 100 times and see ~45% improvement in IE, whether it’ll be any better for you in the real world I’m not sure:

    function focusFirstItem (divId)
    {
        if ($(divId))
        {
            var elems = $('myForm').getElements();
            var elems2 = $(divId).select('*');
            for (i=0, l=elems2.length; i<l; ++i) {
                var item = elems2[i];
                if (item.id && !item.disabled && !item.readOnly && item.visible) {
                    if (elems.find(function(s){return s.id = item.id}))
                    item.focus();
                    break;
                }
            }
        }
    }
    

    In example page with rudimentary timing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 392k
  • Answers 392k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The C++0x spec (§[dcl.fct.def.delete]) doesn't deny such constructs, and g++… May 15, 2026 at 1:35 am
  • Editorial Team
    Editorial Team added an answer With a transaction. start transaction; insert into archive select *… May 15, 2026 at 1:35 am
  • Editorial Team
    Editorial Team added an answer You can use event bubbling to your advantage here, if… May 15, 2026 at 1:35 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.