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:

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

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 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
  $('.Image1.selected').removeClass('selected');
  // add classs to current selection
  $(this).addClass('selected')
});
.Image1 {
  border: 1px solid #ccc;
  margin: 1em;
  padding: 1em;
}

.Image1.selected {
  border: 3px solid orange
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="Image1">One</div>
<div class="Image1">Two</div>
<div class="Image1">Three</div>
<div class="Image1">Four</div>

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

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

Leave a Reply