views:

489

answers:

2

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.

A: 

The problem you have is related to the scope of inp. When you use the var keyword you scoped it to that block.

Try this code:

inp.onkeyup = function() {
  var local_inp_id = inp.id;
  var local_next = next; 
  return function(){ 
    moveOnMax(inp_id, next); 
  }
}();
Tim
Thanks for the quick response, it is greatly appreciated.I see what you mean about the scope and changed my code appropriately, however.... I must be doing something else wrong here cause the new input is created, but the focus is not being set and the event is not being tied to the new input; which tells me I have something wrong elsewhere.Is there a better way of doing this than how I am doing it? It can't be this difficult to create a control and then set the focus on it, but I cannot get this to work correctly.
Jaden
I think you just want to return inp.id instead of next in your createTextBox function. That should fix things for you. I would probably rewrite this without keeping track of next, but if you think you need it for some reason by all means keep it.
Tim
If I don't keep track of what the next id should be, what would I pass to moveOnMax? I guess I could just substring the id of the last one and then add 1 to it.BTW - I changed the return and I still get the same results. Maybe it is the next that is throwing it off.
Jaden
I modified the code to what I think you're looking for and added it above. If you have questions let me know.
Tim
This is perfect. It looks so simple when you see it. Thanks for all your help!
Jaden
A: 
 <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>
Tim

related questions