I have a simple JavaScript expanding and collapsing text on the click of a button. What I want to do is add a "+" sign next to the button and change it to "-" when the text is collapsed and vice-versa.
This is my HTML:
<button onclick="expand('one')">First text</button><span class="plusminus">+</span>
<div class="text" id="one">
A bunch of text here
</div>
<p><button onclick="expand('two')">Second text</button><span class="plusminus">+</span>
<div class="text" id="two">
More text here
</div>
<p><button onclick="expand('three')">Third text</button><span class="plusminus">+</span>
<div class="text" id="three">
And some text here
</div>
This is my CSS:
.text {
display: none;
}
And this is JavaScript:
function expand(textId) {
var e = document.getElementById(textId);
var sign = document.getElementsByClassName("plusminus");
if (e.style.display === "block") {
e.style.display = "none";
sign.innerText = "+";
} else {
e.style.display = "block";
sign.innerText = "-";
}
}
The expand/collapse works, but not changing the sign... What am I doing wrong?
Thanks a lot!