0

So we have 4 buttons and when we click on one it gets a black border but when you click another button the previous button loses its border and the new one gets it.

const btnPlaces = document.querySelectorAll('.btn-places');

for (let i = 0; i < btnPlaces.length; i++) {
  btnPlaces[i].addEventListener("click", function(e) {
    let prevBtn = document.querySelector(".checked");
    if (prevBtn) {
      prevBtn.classList.remove("checked");
      e.target.classList.add("checked");
    } else {
      e.target.classList.add("checked");
    }
  });
}
.checked {
  border: 3px solid rgb(34, 34, 34);
}
<button class="btn-places" id="berlin">BERLIN</button>
<button class="btn-places" id="netherlands">NETHERLANDS</button>
<button class="btn-places" id="sweden">SWEDEN</button>
<button class="btn-places" id="italy">ITALY</button>

Is there an alternative solution that is more efficient?

Millhorn
  • 2,953
  • 7
  • 39
  • 77

3 Answers3

0

You can use :focus state in CSS. Only one element is active at the time with :focus state.

.btn-places:focus {
  border: 3px solid rgb(34, 34, 34);
}
<button class="btn-places" id="berlin">BERLIN</button>
<button class="btn-places" id="netherlands">NETHERLANDS</button>
<button class="btn-places" id="sweden">SWEDEN</button>
<button class="btn-places" id="italy">ITALY</button>
Nick Vu
  • 14,512
  • 4
  • 21
  • 31
  • wow that was easy , but i guess you cant scale it from there. how do you select the one button with focus so you can get data and manipulate it? –  Sep 09 '22 at 15:28
  • 1
    You can use `onClick` for every click on those buttons. If you want to keep the selected button, I think radio button is more suitable for you :D @NickGlav – Nick Vu Sep 09 '22 at 15:45
0

You can use an input radio to it, it will provide you a pseudo-selector ::checked where you can style as you wish It's provide a onchecked callback function too to use your javascript to do something

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/radio

0

It sounds like you're trying to keep the button border even when another is clicked. In that case, you would need to remove:

prevBtn.classList.remove("checked");

from your condition.

const btnPlaces = document.querySelectorAll('.btn-places');

for (let i = 0; i < btnPlaces.length; i++) {
  btnPlaces[i].addEventListener("click", function(e) {
    let prevBtn = document.querySelector(".checked");
    if (prevBtn) {
      e.target.classList.add("checked");
    } else {
      e.target.classList.add("checked");
    }
  });
}
.checked {
  border: 3px solid rgb(34, 34, 34);
}
<button class="btn-places" id="berlin">BERLIN</button>
<button class="btn-places" id="netherlands">NETHERLANDS</button>
<button class="btn-places" id="sweden">SWEDEN</button>
<button class="btn-places" id="italy">ITALY</button>
Millhorn
  • 2,953
  • 7
  • 39
  • 77
  • 1
    It doesn't really sound like he wants to keep border when clicking on another button, it sounds more like his code is working as intended but he just wants to know if there is a better way of doing it – Chris G Sep 09 '22 at 14:31
  • 1
    The OP says, ***when we click on one it gets a black border but when you click another button the previous button loses its border and the new one gets it.*** - That indicates to me the behavior that's happening is what he's trying to avoid. He's knew... just doesn't know how to ask the right question. – Millhorn Sep 09 '22 at 14:39