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

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

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 Report

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 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!