bicycle4431
bicycle4431

Reputation: 82

How to change span ID with each span created in loop

I have the following piece of code:

compareHtml += " </tr><tr>";
      for (i = 0; i < compareArr.length; i++) {
        compareHtml += '<td><a href="" target="_blank">' + compareArr[i].journaltitle + '</a></td>';
        journalTitle.append('<span>' + compareArr[i].journaltitle + '</span> &nbsp;');
      }

And it creates a new span for each journalTitle. However, I want each new span to be created to have the ID "journalTitle1" "journalTitle2" "journalTitle3"....and so on.

Is this something that is possible? Currently they have no ID when created it is just a normal span.

Upvotes: 0

Views: 164

Answers (1)

haidar
haidar

Reputation: 1789

You can do like following

compareHtml += " </tr><tr>";
  for (i = 1; i < compareArr.length-1; i++) {
    compareHtml += '<td><a href="" target="_blank">' + compareArr[i].journaltitle + '</a></td>';
    journalTitle.append('<span id=journalTitle'+i+'>' + compareArr[i].journaltitle + '</span> &nbsp;');
  }

Upvotes: 1

Related Questions