How to allow marking only one image in the datatist?

How using jquery to style selected image in
image gallery based in datalist, so that when you select another picture in gallery, styles from the previous selected image is removed.

<script type="text/javascript">
         $(document).ready(function () {
             $('.Image1').click(function () {
                $(this).css('border', '3px solid orange');


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Toggling a class is simpler to undo than using css() since you can easily target that class as a selector to remove it

$('.Image1').click(function() {
  // remove class from prior selection
  // add classs to current selection
.Image1 {
  border: 1px solid #ccc;
  margin: 1em;
  padding: 1em;

.Image1.selected {
  border: 3px solid orange
<script src=""></script>
<div class="Image1">One</div>
<div class="Image1">Two</div>
<div class="Image1">Three</div>
<div class="Image1">Four</div>

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x