As Fran Islas and Juan Salvador Portugal correctly say, the id attributes are unique, so you should try to do it in another way, this one I propose:
function cambiar() {
// obtener el div principal
const messages_table = document.querySelector("#messagesTable");
// obtener todos los span que estén dentro de un elemento de clase red, del div principal
const all_cambiar_red_span = messages_table.querySelectorAll(".red > span");
// recorrer cada uno de ellos
for (let span of all_cambiar_red_span)
// revisar que no contenga "nocambiar"
if (!span.textContent.includes("nocambiar"))
// cambiar el nombre de la clase del nodo padre(el div) a "white"
span.parentNode.className = "white";
}
.white {
background-color: #FFFFFF;
}
.red {
background-color: #FF0000;
}
<div id="messagesTable">
<div class="white"><span>white --- ggg</span></div>
<div class="red"><span>red --- cambiar</span></div>
<div class="red"><span>red --- nocambiar</span></div>
<div class="red"><span>red --- cambiar</span></div>
<div class="white"><span>white --- hola</span></div>
<div class="white"><span>white --- hola</span></div>
</div>
<button onclick="cambiar()">Cambiar color</button>
I guess when you said
If the span has as value the text "change" then to that div change ...
You used to say that if said content of the div, it said to change with spaces, since all the span contain in itself the text "change", evaluate change and not change in a regex /cambiar/
and both will match: change and not change
That's why I looked for the string "notchange" instead of "change" and then I denied it.
I hope I have helped you.