5

I am building a web form where there is a list of inputs. So far, so good.

However, how can I add a link/button to add say another 5 fields to the list. eg:

<input>
<input>
<input>
<input>

<a href="" onclick="">Add 10 more fields</a>

I have looked through many similar q's on this site, however none seem to describe how to add multiple fields.

Thanks for any help,

Harley

EDIT: Thanks all for help, looks like jquery append is the way to go. However I appreciate the alternative that does not use jquery.

0

7 Answers 7

6
  $("#anyDiv").html('<input id="id1"/>');

....

 $("#anyDiv").append('<input id="id2"/>');
Sign up to request clarification or add additional context in comments.

2 Comments

@harley : Welcome .. That Tick and Upvote just made me cross 1K.. Enjoying SO :)
@harley - I just discovered today that $("body").append(someContent) does not work in IE7 while $(someContent).appendTo("body") does work. Quirky I know, but it reminded about this question and thought I'd come back and update it.
2

Here's a js fiddle showing the example of adding 10 inputs using jQuery's append() function. I suggest only doing one addition per click as in user1301840's example though.

HTML

<div id="inputList"></div>
<input type="button" value="Add 10 Inputs" id="addInputs" />​

Javascript

$('#addInputs').click(function() {
    for (i = 0; i < 10; i++) {
        $('#inputList').append('<input type="text" />');

    }
});​

3 Comments

Sorry, another question- how could I make them into a list so they all sit on the left?
@harley: You could use display: block for the inputs. Then each would have its own line.
Pay attention when copying code, I've gone crazy because of a invisible character at the end of the block.
2

While you already have answers, I thought I'd try and provide a non-jQuery solution:

function addInput(insertAt, formEl, num, inputType, defaultVal) {
    console.log(formEl);
    if (!formEl) {
        return false;
    }
    else {
        num = num ? num : 5;
        var fieldset = document.createElement('fieldset'),
            newInput;
        for (var i = 0; i < num; i++) {
            newInput = document.createElement('input');
            newInput.value = defaultVal || '';
            newInput.type = inputType || 'text';
            fieldset.appendChild(newInput);
        }
        formEl.insertBefore(fieldset, insertAt);
    }
}

var formEl = document.getElementById('theForm'),
    aElem = document.getElementById('aElement');

if (window.addEventListener) {
    // up to date browsers
    aElement.addEventListener('click', function() {
        addInput(this, formEl, 10);
    }, false);
} else if (window.attachEvent) {
    // legacy IE, <9
    aElement.attachEvent('onclick', function() {
        addInput(this, formEl);
    });
}​

JS Fiddle demo.

I don't have IE to test, but the above should work with IE < 9 using the attachEvent fall-back, and >= 9, I think, implements the standard addEventListener().

References:

2 Comments

You're perfectly welcome; hopefully it'll be of some use to you, or others. =)
I think I'm going to use the jquery append. I'll use your solution I I ever have to do this again with using the library!
1

try use the JQuery .append() function

$("a").click(function(e){
 $("form").append("<input />");
})

Comments

1

Yep, jquery is the way to go here.

$("<input type='text'>").appendTo("#someContainer");

Good luck.

Comments

0

The following example add input box at click a link:

<div id="start">
    <input/><br/><br/>
</div> 
<a href="#" id="box">Add more fields</a>

$("#box").click(function(){
    var add="";
    add+='<input/>'+'<br/>'+'</br/>';
    $("#start").append(add);
});

Comments

0

<script type='text/javascript'>
    var counter = 1;
    var limit = 3;
    function addInput(divName) {

        var newdiv = document.createElement('div');
        newdiv.innerHTML = "<br><input type='text' name='myInputs" + counter + "'> <input type='text' name='myInputs" + counter + "'> <select><option value='88888'>888888888</option></select>";
        document.getElementById(divName).appendChild(newdiv);
        counter++;
    }
</script>

<form method="POST">
    <div id="dynamicInput">

        <input type="button" value="Add another text input" onclick="addInput('dynamicInput');">
</form>

1 Comment

Adding some explanation in English would be great

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.