1

I want to be able to enter a number into a text box and then on a button click generate that number of text boxes in another div tag and automatically assign the id

Something like this but not sure how to generate the text boxes and assign automatically assign the id

function textBox(selections) {

    for (i=0; i < selections +1; i++) {
        document.getElementById('divSelections').innerHTML = ("<form><input type="text" id="1" name=""><br></form>");
    }

}
2
  • What is the value of selections here? Commented Apr 9, 2014 at 11:10
  • have a look at my answer. hope it will help you to solve your problem. Commented Apr 9, 2014 at 11:15

7 Answers 7

2

Try this one:

function textBox(selections){
    selections = selections*1; // Convert to int
    if( selections !== selections ) throw 'Invalid argument'; // Check NaN
    var container = document.getElementById('divSelections'); //Cache container.

    for(var i = 0; i <= selections; i++){
        var tb = document.createElement('input');
        tb.type = 'text';
        tb.id = 'textBox_' + i; // Set id based on "i" value
        container.appendChild(tb); 
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

A simple approach, which allows for a number to be passed or for an input element to be used:

function appendInputs(num){
    var target = document.getElementById('divSelections'),
        form = document.createElement('form'),
        input = document.createElement('input'),
        tmp;
    num = typeof num == 'undefined' ?  parseInt(document.getElementById('number').value, 10) : num;
    for (var i = 0; i < num; i++){
        tmp = input.cloneNode();
        tmp.id = 'input_' + (i+1);
        tmp.name = '';
        tmp.type = 'text';
        tmp.placeholder = tmp.id;
        form.appendChild(tmp);
    }
    target.appendChild(form);
}

Called by:

document.getElementById('create').addEventListener('click', function(e){
    e.preventDefault();
    appendInputs(); // no number passed in
});

JS Fiddle demo.

Called by:

document.getElementById('create').addEventListener('click', function(e){
    e.preventDefault();
    appendInputs(12);
});

JS Fiddle demo.

The above JavaScript is based on the following HTML:

<label>How many inputs to create:
    <input id="number" type="number" value="1" min="0" step="1" max="100" />
</label>
<button id="create">Create inputs</button>

<div id="divSelections"></div>

1 Comment

+1 I didn't saw this code before I post my answer.. clone element is the highlight in your code. May I know does it have any performance than creating a new element every time(in my above code)?.
0

See below code sample :

<asp:TextBox runat="server" ID="textNumber"></asp:TextBox>
<input type="button" value="Generate" onclick="textBox();" />
<div id="divSelections">
</div>
<script type="text/javascript">
    function textBox() {
        var number = parseInt(document.getElementById('<%=textNumber.ClientID%>').value);

        for (var i = 0; i < number; i++) {
            var existingSelection = document.getElementById('divSelections').innerHTML;
            document.getElementById('divSelections').innerHTML = existingSelection + '<input type="text" id="text' + i + '" name=""><br>';
        }
    }

</script>

Note: Above code will generate the N number of textboxes based on the number provided in textbox.

Comments

0

It's not recommended to user innerHTML in a loop :

Use instead :

function textBox(selections) {
    var html = '';
    for (i=0; i < selections +1; i++) {
        html += '<form><input type="text" id="'+i+'" name=""><br></form>';
    }
    document.getElementById('divSelections').innerHTML = html;
}

And be carefull with single and double quotes when you use strings

Comments

0

You have to change some code snippets while generating texboxes, Learn use of + concatenate operator, Check code below

function textBox(selections) {
    for (var i=1; i <= selections; i++) {
        document.getElementById('divSelections').innerHTML += '<input type="text" id="MytxBox' + i + '" name=""><br/>';
    }
}
textBox(4); //Call function

JS Fiddle

Some points to taken care of:

1) In for loop declare i with var i
2) your selection + 1 isn't good practice at all, you can always deal with <= and < according to loop's staring variable value
3) += is to append your new HTML to existing HTML.

Comments

0

ID should be generate manually.

var inputName = 'divSelections_' + 'text';
for (i=0; i < selections +1; i++) {

    document.getElementById('divSelections').innerHTML = ("<input type='text' id= " + (inputName+i) + " name=><br>");
}

edit : code formated

Comments

-1

Instead of using innerHTML, I would suggest you to have the below structure

HTML:

<input type="text" id="id1" />
<button id="but" onclick="addTextBox(this)">click</button>
<div id="divsection"></div>

JS:

function addTextBox(ops) {
    var no = document.getElementById('id1').value;
    for (var i = 0; i < Number(no); i++) {
        var text = document.createElement('input'); //create input tag
        text.type = "text"; //mention the type of input
        text.id = "input" + i; //add id to that tag
        document.getElementById('divsection').appendChild(text); //append it
    }
}

JSFiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.