1đ
There are a couple of ways to do this.
You can recreate the form object using JavaScript. Then each time you need to add a form, you just rerun the JavaScript function.
Alternatively, you can create the form using regular HTML and assign it âhiddenâ and the class âoriginalâ. Then each time you want to add a row, you run the following:
function(){
var newForm = $('form#blockForm.original').clone().removeClass('original');
newForm.appendTo($('div.container')).show();
}
To run this function, you might wrap it in a .click() event attached to the âAdd Rowâ button. Donât use <a href="#" id="insert-more"> Add New Row </a>
. Instead, I prefer to go with the following:
HTML:
<span id="insert-more">Add New Row</span>
JavaScript:
$('span#insert-more').click(function(){
var newForm = $('form#blockForm.original').clone().removeClass('original');
newForm.appendTo($('div.container')).show();
});
And naturally, you need something to activate this javascript after the span has loaded. I use $(document).ready(function(){//This is where most of my JavaScript file goes.});
âEDITâ
Okay, I suppose I wasnât very clear up at the top. I was suggesting that you use a sort of master form thatâs hidden from view, with the class being âoriginalâ. Then when you copy this, the code I wrote removes that class, but only from the copy. This is so that you do not copy any of the copies. But you do need to put the class âoriginalâ in the original form that you want to copy! You also want to do away with most of the ids in the form, or simply donât use them at all. I prefer to use classes for this, and then anything I do with the form is usually stated as follows.
$('button.formSubmit').click(function(){
var $thisForm = $(this).parent().parent();
//multiple parent functions may be chained together, depending on how far down the button is nested.
//examples of use:
$thisForm.children('input.blah1').doSomething();
var inp2 = $thisForm.children('input.blah2');
//Really, you can do anything at this point.
});
As for giving the forms unique ids, it is unnecessary so long as you have a way to figure out what belongs to which form, which is simple with the functionality of $(this)
which identifies what thing was clicked on and works based off that. Also, if you actually use the standard âsubmit formâ functionality, you donât have to worry about $(this)
because it submits the form in which the button was clicked, regardless of id or class. You only need to worry about ids when youâre going to put something outside of a form that relates to something inside a particular form. But since youâre making duplicate forms, you wouldnât want to do that anyways.
0đ
I have created a plunker for you which clones your html form.
$(function(){
$("#insert-more").click(function(){
var form = $("#blockForm").clone();
$(".container").append(form);
});
});
- [Answer]-Stale content type prompt deleting all model instances after renaming django model with permissions
- [Answer]-Django at extra_context 'unicode' object does not support item assignment
0đ
Are you sure you want to clone the form or just individual rows inside a single form? If itâs really the latter, you can use a function to duplicate the fieldsets within your form and update the input field names to match.
function dupeForm() {
var reqs = document.getElementsByClassName('requestForm'),
btn = document.getElementById('submitButton'),
len = reqs.length,
lastReq = reqs[len - 1],
newReq = lastReq.cloneNode(true),
txtBxs = newReq.getElementsByTagName('input');
for (var i = 0; i < txtBxs.length; i++) {
txtBxs[i].setAttribute('name', 'txt' + len);
txtBxs[i].setAttribute('id', 'txt' + len);
txtBxs[i].value = 'More Text';
}
lastReq.parentNode.insertBefore(newReq, btn);
}
See the fiddle here: http://jsfiddle.net/vp84oodt/
- [Answer]-How to manage media files on Windows?
- [Answer]-Django-Haystack autoquery gives strange results with solr backend