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>

Answers:

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
  $('.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>


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

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x