The code below is supposed to replace all of the array values with a span that formats it to a different colour. Unfortunately, this is only working for the fifth array value and not any others. I am unsure where the mistake is. Code and fiddle are below:
var names = ["JakeP97", "Trishy", "Puffs", "Evilgenious", "Joeyc", "TheKid"];
var namecolours = ["red", "pink", "yellow", "white", "green", "blue"];
var tribechat = document.getElementsByClassName('tribeconvo');
for (var i = 0; i < tribechat.length; i++) {
colourallocate(names[i], namecolours[i]);
}
function colourallocate(chatname, chatcolour) {
var s = tribechat[i].innerHTML;
s = s.replace(chatname, '<span style="color:' + chatcolour + '">' + chatname + '</span>');
tribechat[i].innerHTML = s;
}
<div class="tribeconvo">Joeyc: hey everyone</div>
<div class="tribeconvo">JakeP97: hello joey</div>
<div class="tribeconvo">Joeyc: oi m8, whats up</div>
<div class="tribeconvo">TheKid: LOL hey JakeP</div>
<div class="tribeconvo">Joeyc: RIP</div>
names[0]intribechat[0],names[1]intribechat[1], and so on. The only one where the name matches the DIV istribechat[5].Joeycis the 5th value in the array and his name is in the 5th instance of classtribeconvo?