Tips for displaying a checkmark icon once an image has been chosen

When selecting images one by one and highlighting them with border:2px solid #b404ae;, I want to place a tick image at the bottom of the selected images. However, my attempt was unsuccessful as only the border color is showing. Can anyone provide me with a solution for this issue?

CSS:

.highlighted {
    border: 2px solid #b404ae;
    background-image: url(image/tick.png);
}

Javascript:

<script type="text/javascript">

var ImageSelector = function() {
    var imgs = null;
    var selImg = null;

    return {
        addImages: function(container) {
            imgs = container.getElementsByTagName("img");
            for (var i = 0, len = imgs.length; i < len; i++) {
                var img = imgs[i];
                img.className = "normal";
                img.onclick = function() {
                    if (selImg) {
                        selImg.className = "normal";
                    }
                    this.className = "highlighted";
                    selImg = this;
                };
            }
        }
    };
}();

</script>

<script type="text/javascript">

var div = document.getElementById("textbox");
ImageSelector.addImages(div);

</script>

Answer №1

One option is to place a span tag beside the image and have it positioned over the image. You can include the HTML code (&#10004;) for a tick mark within the span. Here's a slight modification of your function.

Check out the JSFiddle sample code: http://jsfiddle.net/hz8q9z17/3/

var ImageSelector = function() {
  var imgs = null;
  var selImg = null;
  return {
    addImages: function(container) {
      imgs = container.getElementsByTagName("img");
      for (var i = 0, len = imgs.length; i < len; i++) {
        var img = imgs[i];
        img.className = "normal";
        img.nextSibling.className = "normal";
        img.onclick = function() {
          if (selImg) {
            selImg.className = "normal";
            selImg.nextSibling.className = "normal";
          }
          this.className = "highlighted";
          this.nextSibling.className = "highlighted";
          selImg = this;
        };
      }
    }
  };
}();
var div = document.getElementById("textbox");
ImageSelector.addImages(div);
.highlighted {
    border:2px solid #b404ae;
    background-image: url(image/tick.png);
}
#textbox span {
    bottom: 4px;
    color: #0f0;
    left: -15px;
    position: relative;
    display:none;
    border:none;
}
#textbox span.highlighted {
    display:inline;
    
}
<div id="textbox">
    <img src="http://www.flags.net/images/smallflags/CHIN0001.GIF" /><span class="tickmark">&#10004;</span><br />
  <img src="http://www.flags.net/images/smallflags/UNST0001.GIF" /><span class="tickmark">&#10004;</span><br />
  <img src="http://www.flags.net/images/smallflags/INDA0001.GIF" /><span class="tickmark">&#10004;</span><br />
  <img src="http://www.flags.net/images/smallflags/UNKG0001.GIF" /><span class="tickmark">&#10004;</span><br />
</div>

I hope this solution is helpful.

Answer №2

           var ImageSelector = function() {
              var imgs = null;
              var selImg = null;
              return {
                 addImages: function(container) {

                    imgs = container.getElementsByTagName("img");
                    for(var i = 0, len = imgs.length; i < len; i++) {
                       var img = imgs[i];

                       img.className = "normal";
                       img.onclick = function()  {
                          if(selImg)   {
                             selImg.className = "normal";
                            selImg = null;
                           }
                            this.className = "highlighted";
                            selImg = this;
                        

                         
                        
                       };
                    }
                 }
              };
           }();

              var div = document.getElementById("textbox");
              ImageSelector.addImages(div);
.highlighted {
  border: 2px solid #b404ae;
}

img {
 height: 40px;
  width: 122px; 
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
  <div id="textbox">
    <img src="https://www.example.com/image1.png">
    <img src="https://www.example.com/image2.png">
    <img src="https://www.example.com/image3.png">
    <img src="https://www.example.com/image4.png">
  </div>
</body>
</html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Choose specific elements based on their attribute within a class, and then either apply or remove a class

I have a project where I need to store the data information of each element in my navigation menu when a button is clicked. This project is a one-page website with two levels of navigation: Main menu Button at the bottom of each "page" Currently, I hav ...

"Discover the power of Jade template engine: A guide to effortlessly setting a stunning background image

Is there a way to add a background-image to my HTML page when using Jade? I would really appreciate any guidance on how to do this. Thank you in advance! ...

The functionality of Intersection Observer causes text to appear over the header

Hey everyone, I've been working on a scrolling animation to make text appear when it's at least 50% visible. So far, I have an animated header with an Onscroll Event and Intersection Observer for the text. It's all working well, except for ...

There was an issue with the SidebarController function being undefined, as it was expected to be a function

I'm encountering two issues with my demo: Error: [ng:areq] Argument 'SidebarController' is not a function, received undefined http://errors.angularjs.org/1.2.26/ng/areq?p0=SidebarController&p1=not%20aNaNunction%2C%20got%20undefined ...

The problem in React arises when the event.target becomes undefined or null

I'm currently using React Hooks and I have encountered an issue while trying to update the state within a function that utilizes event.target and event.name. The problem arises as the event object within the handling function returns undefined for bo ...

The reset function is malfunctioning on the meteor entity

Currently, I am going through an angular-meteor tutorial that can be found here I seem to be facing some issues with my code. Here's what I have: angular.module('socially').controller('PartyDetailsCtrl', function($scope, $statePa ...

Obtain specific text from elements on a website

How can I extract a text-only content from a td tag like 'Zubr Polish Lager 6%'? <td width="35%">Amber Storm Scotch Ale 6% <br/>SIZE/LIFE: 330ml <b>CASE</b> <br/>UOS ...

Discovering the most recently selected element in jQuery

Currently reading a jQuery tutorial from the Head First series and practicing with an example where I need to identify the last selected element. In Chapter 2, there is a task involving four images on a page. When a user clicks on any of these images, the ...

Retain values of JavaScript variables acquired by their IDs even after a page refresh

I have a JavaScript function that retrieves HTML input values and inserts them into a table. However, when I refresh the page, those values are reset to null. How can I prevent this and keep the values visible after a refresh? I believe setting and getting ...

Avoid using the JQuery IIFE outside of the document ready function as it is considered a

Hey there! I've been coming across a puzzling "pattern" lately that doesn't sit well with me. The code seems messy and lacks clear structure. I've included a sample of the code for context. Is declaring all those IIFEs outside the document ...

Should I open the Intel XDK hyperlinks in the default web browser instead of

Lately, I've been developing an HTML5 app on the Intel XDK that includes buttons linking to external websites. However, I'm encountering an issue where the apps open in the same window, leading users to get stuck in the browser. Currently, I am u ...

Update the text on Bootstrap Tooltip when it is clicked

I am looking to update the content of my tooltip when it is clicked. Below is the current code snippet I am using: function myFunction() { var copyText = document.getElementById("myInput"); copyText.select(); document.execCommand("copy"); ...

Iterating through JSON array using Jquery and making POST/GET requests

I can't seem to get my code to loop over the entire JSON array, despite trying multiple tricks and making various changes. Using AJAX to Retrieve Data function FetchData() { $.ajax({ type: "POST", url: "htt ...

adjustable content cursor location

Is there a way to make the cursor automatically appear at the start of a content editable div when clicking on the edit button? Currently, it appears at the end and requires arrow key navigation to reach the beginning for editing. I've tried adding th ...

javascript inquiry about if statements

function checkValidity() { startChecked = false; finishChecked = false; alert("startChecked: " + startChecked); alert("finishChecked: " + finishChecked); if (document.dd.start.checked.length == undefined || document.dd.finish.checked. ...

Using a single Angular component to dynamically load data based on the query string

I am curious if it is feasible to load data based on a query string. For instance, when the user clicks on the following link http://localhost:4200/earning/customers?type=archived, the data will be loaded using the same component CustomersComponent. Simila ...

Deactivate the next button on the paginator within the PrimeNG p-table

In the p-table paginator, I want to disable the next button when a specific boolean is set to false. Below is my p-table setup: <p-table #dt id="{{id}}_table" [(value)]="_datasrc" [columns]="cols" [rows]="rowNumber || ...

Utilize Angular, PHP, and MySQL to add data to database encountering error

Working with a controller to fetch records from a database table and bind them to textfields for insertion into another table. However, upon submission, encountering the following error: TypeError: Cannot read property 'resp_fname' of undefined ...

JSRender: A guide on accessing variables from outside the block

I am currently using jsrender to display the details of my JSON object. One thing I'm trying to figure out is how to access an external variable from within the list. Any help would be greatly appreciated. <script id="itemTemplate" type="text/x ...

Setting up server-side CORS in ExpressJS will not include the "Access-Control-Allow-Origin" header

Looking to tackle a CORS request for an ExpressJS server, which is new territory for me. Despite encountering similar issues in the past, I can't seem to pinpoint the problem this time around. It appears that the required headers may not be in the cor ...