Since you are already using jquery:
for (var i = 0; i < tab.length; i ++) {
tab[i] = $(tab[i]).find("tr").first().attr("id", "id_" + i);
}
What this does:
- Create jquery objects from the html string:
$(tab[i])
- Find the first tr object:
.find("tr").first()
- Set the attribute “id” of the object to “id_” + i:
.attr("id", "id_" + i)
I added “id_” because ids and classes starting with a number are invalid and may be ignored by a browser.
This will result in your array containing jquery objects instead of strings, but you can add them to your DOM just like a string. If you need the result as a string for some reason, you can just add .html()
to return a html String.
0
solved How can I change id in string