[Fixed]-Updating/overwriting HTML data with jQuery?

1👍

So, as we learned – using same id for multiple elements is not allowed. Of course, you can add same ids, but your code

$("#industryInterest")

will select only first one.

That’s why you need to use something else. Use a class industryInterest:

<span class="industryInterest" ....

Next, you want to update not all spans but ones which are is_interested. For getting element with certain index use eq

So, your code becomes:

for (i = 0; i < response.length; i++) { 
   //alert(response[i].industry + ":" + response[i].is_interested);

   if (response[i].is_interested) {
     // we use class here and we're getting 
     // element with the same index as i
     // and you can chain jquery functions too!
     $(".industryInterest").eq(i)
         .html(response[i].industry)
         .attr("data-fit", response[i].industry);
   }
}

Leave a comment