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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:31:52+00:00 2026-05-13T05:31:52+00:00

What I have is a single textbox. If the user hits the maxlength of

  • 0

What I have is a single textbox. If the user hits the maxlength of it, I want to create a new textbox and then change focus to it so they can continue typing.

To accomplish this, I am trying to dynamically create textboxes that have an onkeyup event tied to them. To do this I am using document.createElement and the creation of the element works. The problem is that I can’t get the parameters (the id of the current textbox and the id of the one to be created) to pass correctly and they are simply variables. Before I pass them I can test them and they are fine, but in the method they are null.

Here is my code:

 <script type="text/javascript">
    var i = 2;
    function CreateTextbox() {
  var box = document.getElementById(divCreateTextbox);
        var curr = 'txt' + i;
        var next = 'txt' + (i + 1);

        var inp = document.createElement('input')
        inp.type = 'text';
        inp.name = 'textfield';
        inp.maxlength = '10';
        inp.id = curr;
        inp.setAttribute('onkeyup', 'moveOnMax(inp.id, next)');
        inp.onkeyup = function() { moveOnMax(inp.id, next); };

        box.appendChild(inp);
        box.innerHTML += "<br />";

        i++;

        return next;
    }

    function moveOnMax(field, nextFieldID) {
        if (field.value.length >= field.maxLength) {
            if (document.getElementById(nextFieldID) == null) {
                var id = CreateTextbox();

                if (document.getElementById(id) != null) {
                    document.getElementById(id).focus();
                }
                else
                    alert("problem...");
            }
        }
    }
</script>

   <div id="divCreateTextbox">

I am pretty new to Javascript, so if this is completely fubar’d, I apologize.
Any help is appreciated.

  • 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-13T05:31:53+00:00Added an answer on May 13, 2026 at 5:31 am
     <script type="text/javascript">
        getId = function(){
          var id = 1;
          return function(){
            id++;
          }
        }();
    
        function CreateTextbox() {
            var box = document.getElementById("divCreateTextbox");
            var curr = 'txt' + getId();
            var inp = document.createElement('input');
    
            inp.type = 'text';
            inp.name = 'textfield';
            inp.setAttribute("maxlength",'10');
            inp.setAttribute("id",curr);
    
            box.appendChild(inp);
    
            inp.setAttribute('onkeyup','moveOnMax(this)');
            box.appendChild(document.createElement("br"));
            inp.focus();
        }
    
        function moveOnMax(s){
           if(s.value.length >= parseInt(s.getAttribute("maxlength"))-1){
            s.blur();
            CreateTextbox();
           }
        }
    
    </script>
    
    
       <div id="divCreateTextbox"></div>
    
       <script>
       window.onload = function(){
          CreateTextbox()
        }
       </script>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a single textbox named Keywords. User can enter multiple strings for search.
I need to have a textbox (sort of) where the user can enter text
so I Have a JTextArea for user input and then when they click a
What I want I have an EditText, where the user can enter a value,
To simplify the question, I have a single textbox control which I want to
I have a single user java program that I would like to have store
In my windows phone 7 App I have a single line textbox. When the
I have a textBox control that lets the user their email address/addresses. The use
I have a simple Form with a single TextBox, plus OK and Cancel buttons.
Higuys, I have a Textbox where user choose a date (MM/dd/yyyy) and another TextBox

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.