Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #33741
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: cloneNode |
| Date | 2017-08-09 23:43 +0200 |
| Organization | PointedEars Software (PES) |
| Message-ID | <2417962.mvXUDI8C0e@PointedEars.de> (permalink) |
| References | <e7792c44-f4ee-4706-a5c1-939cfe5b165c@googlegroups.com> <6gyruqx829y4$.1lpqy2p7ncjve$.dlg@40tude.net> |
JJ wrote:
> On Tue, 8 Aug 2017 12:25:50 -0700 (PDT), justaguy wrote:
>> I wonder if there's any way such as inserting a new element with id to a
>> cloned node? Thus, to be able to differentiate them.
>
> Yes. Just assign a new unique ID to the new element after it's created. It
> might be good to use randomly generated ID. e.g.
>
> var newID = ("button_" + (new Date()).valueOf()) +
> Math.trunc(Math.random() * 1000000);
>
> You might want to keep track of the IDs in an array so that you can access
> that new element. Or better, keep the element reference rather than its
> ID.
The built-in `Date` prototype method that should be called here is
`getTime`, not `valueOf`, although both return (and have always returned)
the same value.
<http://www.ecma-international.org/ecma-262/7.0/#sec-date.prototype.valueof>
vs.
<http://www.ecma-international.org/ecma-262/7.0/#sec-date.prototype.gettime>
The `Math` object has a built-in `trunc` method only in newer ECMAScript
implementations. Therefore, the methods used to produce a uniformly
distributed pseudo-random integer should be `floor` or `ceil`:
<http://www.ecma-international.org/ecma-262/7.0/#sec-math-object>
vs.
<http://www.ecma-international.org/ecma-262/5.1/#sec-15.8>
As one can see there, the advantages of calling the `trunc` method instead
of the `floor` method are negligible; the disadvantages are important.
“1e6” might be easier to read than “1000000”.
However, it is much better *here* not to rely on either value, but to use a
custom counter (if that; I can only think of separate `label` elements in
siblings as a valid reason). If all elements have the same name, but should
have different IDs, the current counter value is easily obtained through the
`length` property of the corresponding HTMLCollection. For 1-based
counting:
/*
* Makes `newID` “button_4” if there are already 3 controls named
* “button” in the form represented by the object referred to by `f`
*/
var newID = "button_" + (f.elements["button"].length + 1);
[The ID prefix does not have to be the same as the name.]
--
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
cloneNode justaguy <lichunshen84@gmail.com> - 2017-08-08 12:25 -0700
Re: cloneNode "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-08-09 16:04 +0200
Re: cloneNode JJ <jj4public@vfemail.net> - 2017-08-09 23:26 +0700
Re: cloneNode Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-08-09 23:43 +0200
Re: cloneNode justaguy <lichunshen84@gmail.com> - 2017-08-09 18:48 -0700
csiph-web