Am working on an application whereby I have some data in a Javascript object which I have pulled from the backend. Am trying to iterate through the Javascript object and populate in the DOM inside a ul but it aint working, the ul has existing text that I want to change instantly after loop.
Javascript object copied from browser console tab
0: "Mathematics"
1: "English"
2: "Swahili"
3: "Germany"
Logic
$.each(planDetails, function (key, value) {
$("#packageBenefits ul li" + key).text(value);
});
Markup to populate data into
<div class="card">
<div class="card-header">All subjects</div>
<div class="card-body" id="packageBenefits">
<ul>
<li><span> Mathematics </span> </li>
<li>lorem ipsum ............... </li>
<li>lorem ipsum ............... </li>
<li>lorem ipsum ............... </li>
<li>lorem ipsum .............. </li>
</ul>
</div>
</div>
packageBenefits. No such element exists so you are not updating your text. Rather than looping through the list of values to fill, try properly grabbing the group of li's you want to update and loop through that.plainDetails? Well,$("#packageBenefits ul li" + key)is going to generate something like#packageBenefits ul li1. Maybe you want something like:$("#packageBenefits ul li:nth-child(" + key +")")?<li>elements (one for every entry inplanDetails)? And what isplanDetailsexactly? An object ({}) or an array ([])?