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

  • SEARCH
  • Home
  • 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 6323249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:28:56+00:00 2026-05-24T16:28:56+00:00

The HTML on the page has 20 <input> fields each named and given ID’s

  • 0

The HTML on the page has 20 <input> fields each named and given ID’s in increasing order from 1 to 20.

If the variable id is set to the next sequential id (id + 1), this function will cause focus to apply to that field. However, when clicking outside of the current input field, the last one input field will not regain focus if the number entered is greater than 10, but an alert will be displayed.

$(":input").focusout(function(){
    var input = $(this).val();
    var id    = $(this).attr('id');
    if(input > 10){ 
        alert('You must enter a number between 0 and 10 '+id);
        $("#"+id).select();
    }
});

How can the last input field be set to regain focus?

  • 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-24T16:28:56+00:00Added an answer on May 24, 2026 at 4:28 pm

    Try replacing:

    $("#"+id).select();
    

    With:

    $(this).focus();
    

    In this case, $("#"+id) and $(this) are the same element, and I’m assuming you want to focus the element when there is an error.

    Aside: I don’t believe that id or name values can legally start with a number, you may want to prefix them with something like option1, option2, etc. It might work, but it might also cause issues later that are difficult to debug. Best to err on the side of caution and best practices.

    What are valid values for the id attribute in HTML?

    Edit: After failing to get focus() to work, I tried with setTimeout and was able to make it happen. I’m not sure why, or if this is really necessary, but it seems to work.

    $(":input").focusout(function(){
        var $this = $(this),
            input = $this.val();
    
        if (input > 10){
            alert('You must enter a number between 0 and 10');
            setTimeout(function(){
            $this.focus();
            }, 1); 
        }
    }); 
    

    I’d love to hear if there is a better way to do this or an explanation. I suspect that the blur and focus events are not fired in an order that makes the previous method possible?

    Demo: http://jsfiddle.net/zRWV4/1/

    As mentioned in the comments, you should eventually make sure the value is an integer with parseInt or similar (you seem to already be aware of this).

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

Sidebar

Related Questions

A form on my website's contact -us HTML page has two fields 1) subject
Given an HTML page that has a complex table-based layout and many tags that
I have an html page which has elements such as the following: <input data-required-by=books
I have a page called index.html which takes in a few variable from the
I have an html page which has a link called open. Once the link
I have a html page with a form. The form has Div which gets
I have a page which has an Html.DropDownList which takes in a viewdata parameter.
I have HTML form with lots of select and input fields, using it a
There is a webpage that has Input textfields. I want to launch the page
My ASP.NET MVC 3 site generates and html with scripts around my input fields,

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.