How can i get a unique id for every class items in this js

Here is a text, and when we add content through the textbox, then every new line adds into a new div class with quiz in inner div.

Just Like This:

<div id="inner">
<div class="quiz">this is line one</div>
<div class="quiz">this is second line</div>
<div class="quiz">this is third line</div>
<div class="quiz">and so on...</div>
</div>

But I want to add a unique id in the "n" Number series from "1" on every quiz class Just Like this output I want when we add content through this textbox.
Like That:

<div id="inner">
<div id="1" class="quiz">this is line one</div>
<div id="2" class="quiz">this is second line</div>
<div id="3" class="quiz">this is third line</div>
<div id="4" class="quiz">and so on...</div>
</div>

My Whole Code is:

const sendButton = document.getElementById('send-btn');
const textArea = document.getElementById('input');
const innerDiv = document.getElementById('inner');
var message = textArea.value;

sendButton.addEventListener('click', function() {
  // split the textarea entries into an array
  let lines = (textArea.value).split("\n");

  // iterate over each line, creating a div/span and inserting into the DOM
  lines.forEach( (line) => {
    let encodedLine = encodeHtmlEntity(line);
    let newElement = `<div class="quiz">${encodedLine}</div>`;
    innerDiv.innerHTML += newElement;
  });
  
  // reset the textarea
  textArea.value = '';

});

function encodeHtmlEntity(input) {
  var output = input.replace(/[\u00A0-\u9999<>\&]/gim, function(i) {
    return '&#' + i.charCodeAt(0) + ';';
  });

  return output;
}
<div id="inner"> </div>
<br>
<textarea class="input" id="input" placeholder="Message..."></textarea><br>
<button class="waves-effect waves-light" id="send-btn">Add Content</button>

What some new lines of code I need to write, anyone can help me.

44 thoughts on “How can i get a unique id for every class items in this js”

  1. 598374 618236Excellent weblog here! Furthermore your website rather a good deal up quick! What host are you the usage of? Can I get your affiliate link to your host? I wish my internet site loaded up as quickly as yours lol 959825

    Reply
  2. 857222 548821Over and more than again I like to think about this issues. As a matter of fact it wasnt even a month ago that I thought about this extremely thing. To be honest, what is the answer though? 559278

    Reply

Leave a Comment