How to give unique ids to changing divs?
-
The function displays values of options property of array in separate divs. I want to give each div a unique id, which I have given, but the first displayed divs are replaced by totally different divs on click; so I am unable to give ids to each div. I want that divs should remain same and only the values should change on click. Presently the ids that I have given are removed when next values are displayed on click. How can I achieve that? Thanks for your suggestions.
Options
var antonyms = [
{ question: "soft", options: \["dark", "white", "heavy", "hard", "down", "pretty", "butter", "cotton"\], answer: "hard" }, { question: "beautiful", options: \["pretty", "ugly", "handsome", "wonderful", "up", "high", "cheerful", "black"\], answer: "ugly" }, \] // Define a method that generates the HTML for one question. function showOptions(q) { let qHtml = '
' + q.question + '
';
qHtml += q.options.map(o => '
' + o + '
').join("");
return qHtml; }
```
// Start showing the first question. Click event will show next question.let container = document.getElementById("container"); let button = document.getElementById("btnNext"); let qIdx = 0; container.innerHTML = showOptions(antonyms\[qIdx\]); button.addEventListener("click", function() { qIdx += 1; if (qIdx < antonyms.length) container.innerHTML = showOptions(antonyms\[qIdx\]); else button.disabled = true; });
```
// Giving unique id to each option div$('#container div').each(function(eq, el) { el = $(el); if (typeof(el.attr('id')) === "undefined") { el.attr('id', 'box-' + eq); } });
-
The function displays values of options property of array in separate divs. I want to give each div a unique id, which I have given, but the first displayed divs are replaced by totally different divs on click; so I am unable to give ids to each div. I want that divs should remain same and only the values should change on click. Presently the ids that I have given are removed when next values are displayed on click. How can I achieve that? Thanks for your suggestions.
Options
var antonyms = [
{ question: "soft", options: \["dark", "white", "heavy", "hard", "down", "pretty", "butter", "cotton"\], answer: "hard" }, { question: "beautiful", options: \["pretty", "ugly", "handsome", "wonderful", "up", "high", "cheerful", "black"\], answer: "ugly" }, \] // Define a method that generates the HTML for one question. function showOptions(q) { let qHtml = '
' + q.question + '
';
qHtml += q.options.map(o => '
' + o + '
').join("");
return qHtml; }
```
// Start showing the first question. Click event will show next question.let container = document.getElementById("container"); let button = document.getElementById("btnNext"); let qIdx = 0; container.innerHTML = showOptions(antonyms\[qIdx\]); button.addEventListener("click", function() { qIdx += 1; if (qIdx < antonyms.length) container.innerHTML = showOptions(antonyms\[qIdx\]); else button.disabled = true; });
```
// Giving unique id to each option div$('#container div').each(function(eq, el) { el = $(el); if (typeof(el.attr('id')) === "undefined") { el.attr('id', 'box-' + eq); } });
I don't quite follow where you are lost, but if you want the id to be unique then keep a global variable counter and add it to the end of each div's id and then increment it.
Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.