Path: csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Ben Bacarisse Newsgroups: comp.lang.javascript Subject: Re: How to add/append code to a function? Date: Mon, 11 Apr 2016 22:01:14 +0100 Organization: A noiseless patient Spider Lines: 51 Message-ID: <87egab2739.fsf@bsb.me.uk> References: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="017616aa25f81ec581c44d76d61ba2f3"; logging-data="18747"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+mafN+soKmF3hrd+ssDSHHJy+VhfL1JWA=" Cancel-Lock: sha1:wEQ78n1nawUi5dvay50x6tGEIb4= sha1:7ZEgF6plSv1OL7oEFJ52Kdja2BQ= X-BSB-Auth: 1.6b48a0dea2d84b1f9dca.20160411220114BST.87egab2739.fsf@bsb.me.uk Xref: csiph.com comp.lang.javascript:30262 justaguy writes: > I have an HTML page that is heavy with DOM, that is, I have some table > row with several cells, for instance, > [InstrumentName] [Qty] [Person] [OtherInfo] (as one row) > and have a function, say, AddNewRow(), that allows the user to add additional row(s) > Thus, we have > [InstrumentName] [Qty] [Person] [OtherInfo] > [InstrumentName_n] [Qt_n] [Person_n] [OtherInfo_n] > ... > > > prior to FORM submission. > > Now, I'd like to allow the user to add additional field(s). Thus, it > may appear like this: > [InstrumentName] [Qty] [Person] [OtherInfo] > +AddField (addField()): > upon click, > Field Name: > Field Type: > > And if the user did click on addField function, how do we add/append > this addition to the defined AddNewRow function? > > The AddNewRow function's last two lines look like the following: > parentEl.insertBefore(newrow,p); > } > > So, it looks like we need to do two things for the addField function. > (a) dynamically insert the two labels and two fields for a new field. > (b) insert/append new code right before the AddNewRow function's ending } > > Is this the way to go? No. It's better to make things data-driven rather than edit code on the fly (which you can't do in this case anyway). The AddNewRow function should be an action on a form. The AddNewField function should modify that form. AddNewRow uses the form to determine how many things it needs in a row. Of course you might also need to edit existing rows to reflect the presence of the new field. -- Ben.