Supposed I have a node that looks like this
<a>
<span class="glyphicons glyphicons-collapse"></span>Collapse All
</a>
And I want to toggle it to this
<a>
<span class="glyphicons glyphicons-expand"></span>Expand All
</a>
If I use innerHTML or innerText or textContent it just replaces the text. My first pass at a solution is
function toggleLastTextNode(element, textWhenTrue, textWhenFalse, value) {
element.removeChild(obj.childNodes[1]);
element.insertAdjacentHTML("beforeend", value ? textWhenTrue: textWhenFalse);
}
As I know where the text is, it's always the obj.childNodes[1] so I just remove it and replace it. The function call would be something like this
toggleLastTextNode(element,"Expand","Collapse",true)
With the current HTML is there a better way to do this? I want to avoid innerHTML if I can.
::beforeor::aftermight also do the trick, leaving that semantically useless text out of your HTML