Path: csiph.com!3.eu.feeder.erje.net!feeder.erje.net!news.mb-net.net!open-news-network.org!.POSTED.96.235.197.178.dynamic.wless.lssmb00p-cgnat.res.cust.swisscom.ch!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: Painted into a corner Date: Wed, 09 Dec 2020 17:09:02 +0100 Organization: PointedEars Software (PES) Lines: 74 Message-ID: <2178622.ElGaqSPkdT@PointedEars.de> References: <11650524.O9o76ZdvQC@PointedEars.de> <5415173.DvuYhMxLoT@PointedEars.de> <5663081.lOV4Wx5bFT@PointedEars.de> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit Injection-Info: gwaiyur.mb-net.net; posting-host="96.235.197.178.dynamic.wless.lssmb00p-cgnat.res.cust.swisscom.ch:178.197.235.96"; logging-data="20784"; mail-complaints-to="abuse@open-news-network.org" User-Agent: KNode/4.14.10 Cancel-Lock: sha1:bVMn0hE+afuZCFKLExnQf4QFQv4= X-User-ID: U2FsdGVkX1+mOWflt1ZGFyJO08+rFM1Y8JubFgiKSPVDxMwVAzrvWw== X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. Rather than making multiple copies of the same "Image" component I was > thinking to just use one and set/reset it when the user adds another > image. > > But currently all the UI components are "within" a single form so if > the user adds an "Image" there's no way it's values can be separated > from any other "Image's" values. Why not? AISB, form controls with the same name are automatically indexed in the DOM. Suppose your “Image” component looks like this:
Then if you have another component
then the controls of the first component can be referred to by form.elements['imgWidth'][0] form.elements['imgGrayscaleCbx'][0] form.elements['imgGrayscaleRange'][0] and the controls of the second component by form.elements['imgWidth'][1] form.elements['imgGrayscaleCbx'][1] form.elements['imgGrayscaleRange'][1] whereas previously let form = document.forms['f-images']; [Historically one would have used names like “imgWidth[]” to indicate the NodeList; IIRC, it still makes a difference in PHP.] It does not matter if the second component was already there (in the markup) or if the user performed an action so that it was inserted into the document tree. And as the NodeList is live, it does not matter if the user deletes the first component. So you can iterate over one of those NodeLists, e.g. form.elements['imgWidth'], to access (and serialize) the corresponding controls. > Should I just clone the "Image" so that form elements exist for every > "Image"? (Though for 10 Images that means 1,000+ inputs added to the > HTML.) If this is not supposed to work without scripting anyway, and requires no user interaction then you should store the data in an array right away, and forget about form serialization. Otherwise it is a good idea to add a new instance of the component. If you do that by cloning markup, then you should clone properly initialized template markup and not the markup of another instance of the component. If none of this makes sense to you, then you have to elaborate on your use-case. -- PointedEars FAQ: | | Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.