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


Groups > comp.lang.javascript > #33719 > unrolled thread

cloneNode

Started byjustaguy <lichunshen84@gmail.com>
First post2017-08-08 12:25 -0700
Last post2017-08-09 18:48 -0700
Articles 5 — 4 participants

Back to article view | Back to comp.lang.javascript


Contents

  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

#33719 — cloneNode

Fromjustaguy <lichunshen84@gmail.com>
Date2017-08-08 12:25 -0700
SubjectcloneNode
Message-ID<e7792c44-f4ee-4706-a5c1-939cfe5b165c@googlegroups.com>
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.

Thanks.

[toc] | [next] | [standalone]


#33728

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2017-08-09 16:04 +0200
Message-ID<XnsA7CCA3728C825eejj99@194.109.6.166>
In reply to#33719
justaguy <lichunshen84@gmail.com> wrote on 08 Aug 2017 in
comp.lang.javascript: 

> 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. 

You must by now know an is is only valid once.

So your code MUST id the clone nd its descendants with unique ids,
or clear ab=ny duplicate.


-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#33732

FromJJ <jj4public@vfemail.net>
Date2017-08-09 23:26 +0700
Message-ID<6gyruqx829y4$.1lpqy2p7ncjve$.dlg@40tude.net>
In reply to#33719
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.

[toc] | [prev] | [next] | [standalone]


#33741

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2017-08-09 23:43 +0200
Message-ID<2417962.mvXUDI8C0e@PointedEars.de>
In reply to#33732
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.

[toc] | [prev] | [next] | [standalone]


#33748

Fromjustaguy <lichunshen84@gmail.com>
Date2017-08-09 18:48 -0700
Message-ID<5dc27edb-a195-4886-9c0e-de192b6c8764@googlegroups.com>
In reply to#33732
On Wednesday, August 9, 2017 at 12:27:06 PM UTC-4, 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.

Thank you all, the problem has been resolved with JJ's recommendation of using class assignment for each layer or group.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web