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 157897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:35:09+00:00 2026-05-11T10:35:09+00:00

I want to set focus on the first form element in a div, either

  • 0

I want to set focus on the first form element in a div, either using something that Prototype gives me, or plain javascript. I have a reference to a div which contains the form elements. I don’t know if the first form element will be an <input>, <select>, or <textarea> node.

I want to do something like this:

$('parentDiv').down('textarea, select, input').focus(); 

However, the Element.down method seems to pick the first textarea node, even though there is an input node before the textarea.

Is there an elegant way around this? I suppose I could get all the descendants of the parent div, and then iterate over each one looking for one of these nodes, but I suspect that there might be a cool way to do this in prototype.

I can’t use the methods of the Form object, because this parent div is only part of the form. It is added to the page via an AJAX call, so when the div has been added to the DOM, I want to set focus on the first field in the newly added set (which is most likely not the first field in the whole form).

Thanks!

  • 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. 2026-05-11T10:35:10+00:00Added an answer on May 11, 2026 at 10:35 am

    When I used prototype i’m sure I did something like this.

    var firstElement = Form.findFirstElement(document.forms[0]); if(firstElement !=null)     firstElement.activate(); 

    I would substitute your form for document.forms[0]

    Prototype also has the method of focusFirstElement on the Form class, but I couldn’t use that because some pages I would load didn’t have forms. You might be able to just call Form.focusFirstElement($(form)); or a similar variation.

    Edit:
    I thought you could use the Form extensions on different types of elements but it doesn’t appear to work, however you can take something from Form and apply it directly. The following code should work as you intend.

    var firstElement = Form.getElements($('parentDiv')).find(function(element) {     return element.type != 'hidden' && !element.disabled &&     ['input', 'select', 'textarea'].include(element.tagName.toLowerCase()); }); if(firstElement != null)     firstElement.activate(); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to set something up so that if an Account within my app
I want to set a background image for a div, in a way that
What I want to do is set the focus to a specific control (specifically
I want to set up an ASP.NET custom control such that it has a
I want to set all the fields and labels on a VFP7 report to
I want to set a breakpoint on the __DoPostBack method, but it's a pain
I want to set up a Subversion server installation on Windows Server 2003 64-bit
I want to set some attributes just before the object is serialized, but as
I want to set registry keys in following way if OS is Vista then
I want to set up an automatic rsync job to backup a bunch of

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.