Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!