2

I have the following HTML code:

<div id="ID_unique"></div>

<span class="a_random_class">a</span>
<span class="a_random_class">b</span>
<span class="a_random_class">c</span>

And the Javascript

<script>
setTimeout(function() {

  var element = document.getElementById("An_ID");  

  Array.prototype.forEach.call(document.querySelectorAll(".a_random_class"), function(e) {

    var example = element.appendChild(e.cloneNode(true));
        example.className += " timeline-date";

    var div = document.createElement('div');
        div.id = "id_frame";

    div.appendChild(example);   
    element.appendChild(div);

  });
}, 300);
</script>

It will wrap the result of the cloneNode inside a HTML div, and those generated div tags will have the same ID id_frame ... However since in HTML all IDs must be unique, how can I tell Javascript to generate a unique ID for each result?

5 Answers 5

1

use counter as global variable and append that to your id and increment counter after assigning id like below:

<script>
setTimeout(function() {

  var element = document.getElementById("An_ID");  
   var counter = 0;
  Array.prototype.forEach.call(document.querySelectorAll(".a_random_class"), function(e) {

    var example = element.appendChild(e.cloneNode(true));
        example.className += " timeline-date";

    var div = document.createElement('div');
        div.id = "id_frame"+counter;
        counter++;
    div.appendChild(example);   
    element.appendChild(div);

  });
}, 300);
</script>
Sign up to request clarification or add additional context in comments.

5 Comments

I would rather use the index of the forEach, than creating a global variable, see my answere
Hi there @VivekGupta thank you, it indeed works, may I ask, is it possible to use with getElementById()? if so, how?
@DanielFelipe where you want to use as document.getElementById(), I didn't get it
I want to add another cloneNode results to a div with the recently generated ID, something like var another_element = document.getElementById("id_frame"+counter); would that be possible somehow?
yes @Daniel it is possible just use it before incrementing counter
1

In general, constructing dynamic IDs and then finding the corresponding elements again later with getElementById is an anti-pattern, perhaps going back to the bad old jQuery days. Instead, just remember the elements themselves. In your case, something like

var my_divs;

setTimeout(function() {

  var element = document.getElementById("An_ID");  
  var elts    = document.querySelectorAll(".a_random_class");

  my_divs     = Array.prototype.forEach.map(elts, function(e) {
    var example = element.appendChild(e.cloneNode(true));
    example.className += " timeline-date";

    var div = document.createElement('div');
    div.appendChild(example);   
    element.appendChild(div);
    return div;

  });
}, 300);

Now you can simply refer to the elements you created and added with my_divs[i], without having to do something like document.getElementById('id_frame'+i).

1 Comment

Oh, very useful @torazaburo, I greatly appreciate your time, ;P I will check my code. Thank you very much!
0

Just use the index of the forEach loop:

<script>
setTimeout(function() {

var element = document.getElementById("An_ID");  
Array.prototype.forEach.call(document.querySelectorAll(".a_random_class"), function(e, index) {

var example = element.appendChild(e.cloneNode(true));
    example.className += " timeline-date";

var div = document.createElement('div');
    div.id = "id_frame"+index;
div.appendChild(example);   
element.appendChild(div);

});
}, 300);
</script>

Comments

0

You can make a util function:

var uniqueId = (function() {
  var counter = 0;
  return function(prefix) {
    return (prefix ? prefix : '') + (++counter);
  };
})();

console.log(uniqueId()); // 1
console.log(uniqueId()); // 2
console.log(uniqueId('my-prefix-')); // my-prefix-3

Then implement it like this:

setTimeout(function() {

  var element = document.getElementById("An_ID");  

  [].forEach.call(document.querySelectorAll(".a_random_class"), function(e) {
    var div = document.createElement('div');
    div.id = uniqueId('id_frame'); // will be id_frame1, id_frame2, id_frame3 ...

    var example = element.appendChild(e.cloneNode(true));
    example.className += " timeline-date";    

    div.appendChild(example);
    element.appendChild(div);
  });
}, 300);

Comments

0

You can do it by having a global count variable, increment it and add it along with the div id name.

<script> 
var id_cnt = 0;

setTimeout(function() {
var element = document.getElementById("An_ID");  
Array.prototype.forEach.call(document.querySelectorAll(".a_random_class"), function(e) {

var example = element.appendChild(e.cloneNode(true));
    example.className += " timeline-date";

var div = document.createElement('div');
    div.id = "id_frame_" + id_cnt++;

div.appendChild(example);   
element.appendChild(div);

});
}, 300);
</script>

Didn't test, but just try it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.