L-Four
L-Four

Reputation: 13531

Dynamic form elements

I need a form that allows users to dynamically add new fields. In my case, one line contains a label and a textarea, and clicking on the plus sign would add a new line with label and textarea.

I found the plugin http://plugins.jquery.com/project/dynamicField, which does exactly what I want, except for one thing: for each added line, I need the label to contain the number of the line.

Any hints on how this could be done?

Best would probably be to add an event in the plugin like 'AfterAdd' that I can subscribe to and set the label or something?

EDIT: does anyone know a plugin that does this kind of 'dynamic form' thing, where you can add, insert and delete rows?

Thanks, L

Upvotes: 0

Views: 565

Answers (2)

fearofawhackplanet
fearofawhackplanet

Reputation: 53396

I don't understand why you'd use a plugin for this. It's only a couple of lines of code. Something like:

$('#add').click(function() {
    var $container =  $('#container');
    var html ='<label>' 
                + ($container.find('textarea').length + 1) 
                + '</label><textarea></textarea><br />';
    $container.append(html);
});

example here

Upvotes: 2

saarthak
saarthak

Reputation: 1014

You can try using jquery templates for that purpose. Something quite clean and great for dynamic DOM generation/insertion.

Upvotes: 0

Related Questions