tags:

views:

176

answers:

5
+3  A: 

You can create the HTML you want to append to the form and insert it into the appropriate container using jQuery.append().

Sometimes what I'll do is keep a TBODY in my form table and give that TBODY and ID so I can reference it via jQuery to append extra rows/cells/form inputs.

JMP
Technically `append` will stick it on the end, whereas I think the OP wants it in the middle somewhere.
Joel Potter
+1  A: 

I think something like this.

inputNum = 2;

function GrowTable()
{
    var input = $("<tr id=\"fileinput" + inputNum + "\">
        <td></td>
        <td>
            <input type=\"file\" name=\"Attachment" + inputNum + "\" id=\"Attachment" + inputNum + "\" />
        </td>
    </tr>");

    input.insertAfter("#fileinput" + (inputNum - 1));

    inputNum++;
}
Joel Potter
+1  A: 

Keeping a counter might help, something like

$('form').append("<tr id=\"fileinput" + i + "\"><td></td><td><input type=\"file\" /></td></tr>");
Chris Thompson
+4  A: 

This'll get you most of the way there.

var row = $('table tr:last').clone();
$(row).attr('id', 'new_ID');
$('input', row).attr('id', 'new_ID_for_input');
$('table').append(row);
ceejayoz
Good idea with clone, but `append` should be exchanged for `after`.
Joel Potter
@Joel Why? After would place the row after (and thus outside!) the table. Append places it within the table, as a table row should be.
ceejayoz
But append puts it as the last element in the table, where you really want to put it after the previous upload row, and before the addinput row. You're selector would have to change to `'table tr#lastrowid'`. I also just noticed that you are cloning the last row, when you want to clone the second-to-last row (or last input row).
Joel Potter
I'd actually advise pulling 'attach another file' out of the table to make things easier, but you're correct, my quickie solution doesn't account for that yet.
ceejayoz
Oops, I upvoted a comment. Doesn't seem I can un-upvote it??? I think using something like $(this).parent('tr').before(row); would work. Personally I would look at the onchange event (or whatever it is) and not have a button at all.
strager
A: 
Zack Peterson
Should that link be "add" or "more"? Or, should I handle some onchange event and have no such link at all?
Zack Peterson