Dhiren
Dhiren

Reputation: 163

How to dynamically add multiple textboxes in a loop

var addquestion=$(document.createElement('div')).attr("id",'questionp'+c);
/* ... */
for(var i=1; i<=a.value; i++)
{
  addquestion.after().html('<label>Q'+c+'.&nbsp;'+q+'</label> <br /> <input type="'+sel.value+'" name="fanswer" id="fanswer" value="'+answ+'">'+answ+'');
  addquestion.appendTo("#question");
}

This is my for-loop code of JavaScript. I want to multiple text boxes in same div, but it only adds a single textbox.

Upvotes: 0

Views: 1482

Answers (1)

Blazemonger
Blazemonger

Reputation: 92893

You're using .after() incorrectly -- it must have an argument to do anything useful.

Try this instead:

for(var i=1; i<=a.value; i++) {
    var addquestion = $('<label>...').appendTo('#question');
}

or:

for(var i=1; i<=a.value; i++) {
    $('#question').after('<label>...');
}

Upvotes: 1

Related Questions