Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

161
Visualizações
Buttons not deselecting after reclick

Having a single button selected at a time works as it's supposed to, but when a button is clicked, it won't deselect when it's clicked again.

I feel like the solution to this is very simple but I can't figure it out.

  <div id="buttonWrapper">
   <button class="buttonArray"></button>
   <button class="buttonArray"></button>
   <button class="buttonArray"></button>
   <button class="buttonArray"></button>
   <button class="buttonArray"></button>
 </div>

    <script type="text/javascript">

        const buttonElement = document.getElementById('buttonWrapper');
        buttonElement.addEventListener('click', function (e) {
            document.querySelectorAll('#buttonWrapper button').forEach(function (el) {
                el.className = "buttonArray";});
                if (e.target.className === "buttonArray") {
                    e.target.className = "buttonSelect";
                }
                else {
                    e.target.className = "buttonArray";
                }
        });

https://jsfiddle.net/cg6n5ahd/

What's the easiest way to solve this with my code? Thanks in advance.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

So I found out that the way I wanted to do this was actually not possible at all so I had to completely redo it to this technique without CSS. Deselecting even with radios was pretty awkward too as they aren't really designed to be deselected either except for work arounds like this one.

function deselectRadio(rootElement) {
  if (!rootElement) rootElement = document;
  if (!window.radioChecked) window.radioChecked = {};
  window.radioClick = function(e) {
    const obj = e.target,
      name = obj.name || "unnamed";
    if (e.keyCode) return obj.checked = e.keyCode != 32;
    obj.checked = window.radioChecked[name] != obj;
    window.radioChecked[name] = obj.checked ? obj : null;
  }
  rootElement.querySelectorAll("input[type='radio']").forEach(radio => {
    radio.setAttribute("onclick", "radioClick(event)");
    radio.setAttribute("onkeyup", "radioClick(event)");
  });
}
deselectRadio();
<div id="buttonWrapper">
  <input type="radio" name="tabRadio" id="radio1" />
  <input type="radio" name="tabRadio" id="radio2" />
  <input type="radio" name="tabRadio" id="radio3" />
  <input type="radio" name="tabRadio" id="radio4" />
  <input type="radio" name="tabRadio" id="radio5" />
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

const buttonElement = document.getElementById('buttonWrapper');
buttonElement.addEventListener('click', function(e) {

  document.querySelectorAll('#buttonWrapper button').forEach(function(el) {
    e.target.className = e.target.className == 'buttonArray' ? 'buttonSelect' : 'buttonArray';
  });
 
});
body {
  color: #000;
  background: #eee;
}

.buttonArray {
  border-color: darkgray;
  border-style: solid;
  border-radius: 10px;
  padding: 10px;
}

.buttonArray:hover {
  border-color: #a0a0a0;
  background-color: #dbdbdb;
}

.buttonSelect {
  border-color: goldenrod;
  background-color: gold;
  border-style: solid;
  border-radius: 10px;
  padding: 10px;
}

#buttonWrapper {
  flex-direction: row;
  padding: 10px;
  top: 1rem;
  left: 0%;
  width: 100%;
  float: left;
  box-sizing: border-box;
  text-align: center;
  -moz-user-select: none;
  -webkit-user-select: none;
  -ms-user-select: none;
  user-select: none;
  z-index: 1;
  background: transparent;
}
 <div id="buttonWrapper">
   <button class="buttonArray"></button>
   <button class="buttonArray"></button>
   <button class="buttonArray"></button>
   <button class="buttonArray"></button>
   <button class="buttonArray"></button>
 </div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda