I have this div:
<div class="signs" id="signs" onclick="toggle()">+</div>
which displays the positive sign. when clicking on it, I want to change it to the negative sign with html code −
:
function toggle() { var x = document.getElementById("signs"); if (x.textContent === `+`) { x.textContent = `−`; } else { x.textContent = `+`; } }
But toggle
function changes the positive sign to plain text −
and not the negative sign! How can I achieve changing the positive to negative sign using javascript with my structure above?
Advertisement
Answer
can you try
function toggle() { var x = document.getElementById("signs"); if (x.textContent == `+`) { x.textContent = `-`; } else { x.textContent = `+`; } }
this?