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

84
Visualizações
Javascript border change

I am creating a website and I have 3 images with border-radius 50% (circled) and they have all a white border. But I need when I click at for example first one it takes a green border. and then if I click on the second image the first one deselects the green border and gets its default border and the second one now gets the green border...

img {
  border-radius: 50%;
  border-color: white;
}
<img src=".">
<img src=".">
<img src=".">

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

0

Wrap the images in a container element and attach a listener to it so you can use event delegation to catch the click events from the images as they "bubble up" the DOM (rather than adding listeners to all the images), and use a class.

When an image is clicked remove the "green" class from all the images, and add it to the image that was clicked.

const container = document.querySelector('#container');
const images = container.querySelectorAll('img');

container.addEventListener('click', handleClick, false);

function handleClick(e) {
  if (e.target.nodeName === 'IMG') {
    images.forEach(image => image.classList.remove('green'));
    e.target.classList.add('green');
  }
}
img { border: 5px solid white; }
.green { border: 5px solid green; }
<div id="container">
  <img src="https://dummyimage.com/100x100/000/fff" />
  <img src="https://dummyimage.com/100x100/555/fff" />
  <img src="https://dummyimage.com/100x100/aaa/fff" />
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

A CSS-only solution would be to use checkboxes. Then change the style for the selected checkbox with: input:checked + label img { border: syntax green; }

img {
  border-radius: 50%;
  border: 5px solid white;
}

input {
  display: none;
}

input:checked + label img {
  border: 5px solid green;
}



/* for styling purpose only */
body {
  background-color: grey;
}
<input type="radio" id="image-1" name="image" value="huey"> 
<label for="image-1">
  <img src="https://via.placeholder.com/100.jpg">
</label>

<input type="radio" id="image-2" name="image" value="huey"> 
<label for="image-2">
  <img src="https://via.placeholder.com/100.jpg">
</label>

<input type="radio" id="image-3" name="image" value="huey"> 
<label for="image-3">
  <img src="https://via.placeholder.com/100.jpg">
</label>

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