Button text will not toggle

R Kline

I don't know what I'm doing wrong here, I've been reading my JS book and googling and messing with it for a couple hours but I can't for the life of me figure out what I'm doing wrong.

HTML

<button id="sidebarButton" class="sidebarButton" onclick="toggle()"> >> </button>

JS

function toggle() {
      if (document.getElementById("sidebarButton").innerHTML == " >> ") {
        document.getElementById("sidebarButton").innerHTML = " << ";
      } else if (document.getElementById("sidebarButton").innerHTML == " << ") {
        document.getElementById("sidebarButton").innerHTML = " >> ";
      }
    }
s.kuznetsov

To change the text on an element according to the toggle principle, it is not necessary to use the if {} condition.

function toggle(element) {
  element.innerText = element.innerText === '>>' ? '<<' : '>>';      
}
<button id="sidebarButton" class="sidebarButton" onclick="toggle(this)">>></button>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related