so I have been researching this all morning- and I’m pretty sure the code is correct but to explain I am trying to include a table in the dynamically generated input, so need the tr/td in there. I have reviewed the html/wrap/append etc. functions to no avail… 🙁
See the comment there for the full script but really this is the meat of it. Any help would be greatly appreciated.
I tried the wrap and document writes but it doesn’t like it. what am I missing???
$(document).ready(function() {
$('#btnAdd').click(function() {
var num = $('.clonedInput').length; // how many "duplicatable" input fields we currently have
var newNum = new Number(num + 1); // the numeric ID of the new input field being added
//http://charlie.griefer.com/blog/2009/09/17/jquery-dynamically-adding- form-elements/
// create the new element via clone(), and manipulate it's ID using newNum value
var newElem = $('#input' + num).clone().attr('id', 'input' + newNum).wrap('<td />');
// manipulate the name/id values of the input inside the new element
newElem.children(':first').attr('id', 'name' + newNum).attr('name', 'name' + newNum).val('');
newElem.children(':second').attr('id', 'amt' + newNum).attr('name', 'amt' + newNum).val('');
newElem.children(':third').attr('id', 'value' + newNum).attr('name', 'value' + newNum).val('');
newElem.children(':fourth').attr('id', 'test' + newNum).attr('name', 'test' + newNum).val('');
// insert the new element after the last "duplicatable" input field
document.write("<tr>");
$('#input' + num).after(newElem);
document.write("</tr>");
Using
document.write()in$(document).ready()will wipe out your existing document and start a new document. I rather doubt that is what you intend (especially since you appear to be trying to add rows to an existing table).You need to use jQuery functions for inserting new elements into the DOM instead of
document.write(). There are many jQuery calls for manipulating the DOM. You can see some of them here. I’d guess that you want calls likeappend()orafter().