Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #33741

Re: cloneNode

Path csiph.com!aioe.org!news.mb-net.net!open-news-network.org!.POSTED.69.235.197.178.dynamic.wless.lssmb00p-cgnat.res.cust.swisscom.ch!not-for-mail
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: cloneNode
Date Wed, 09 Aug 2017 23:43:32 +0200
Organization PointedEars Software (PES)
Lines 60
Message-ID <2417962.mvXUDI8C0e@PointedEars.de> (permalink)
References <e7792c44-f4ee-4706-a5c1-939cfe5b165c@googlegroups.com> <6gyruqx829y4$.1lpqy2p7ncjve$.dlg@40tude.net>
Reply-To Thomas 'PointedEars' Lahn <cljs@PointedEars.de>
Mime-Version 1.0
Content-Type text/plain; charset="UTF-8"
Content-Transfer-Encoding 8Bit
Injection-Info gwaiyur.mb-net.net; posting-host="69.235.197.178.dynamic.wless.lssmb00p-cgnat.res.cust.swisscom.ch:178.197.235.69"; logging-data="665"; mail-complaints-to="abuse@open-news-network.org"
User-Agent KNode/4.14.2
X-User-ID U2FsdGVkX19vHaIMpx2ZS0oAO8fZSjoMGdems6eRvZQABKCrmhR1rg==
Face iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg==
Cancel-Lock sha1:qJfo8YhcLGWhd5GbpiPO5lvPA/Y=
X-Face %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&.<b';Md8`dH6iqhT)6C^.Px|[=M@7=Ik[_w<%n1Up"LPQNu2m8|L!/3iby{-]A+#YE}Kl{Cw$\U!kD%K}\2jz"QQP6Uqr],./"?;=4v
Xref csiph.com comp.lang.javascript:33741

Show key headers only | View raw


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


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