Skip to content
Advertisement

changing text inside a div to negative html code using javascript

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 === `&#43`) {
    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?

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement