Iterating through images one time and capturing the mouse coordinates for every click made by the user

I have the following code snippet that displays a series of images and I would like to capture the coordinates of each mouse click on these images. Is there a way to send these coordinates to my email at the end of the image loop? Any assistance in achieving this functionality would be greatly appreciated.

Below is the script I'm currently using:

<head>
    <title>Practice2</title>
    <link type="text/css" rel="Stylesheet" href="MemStyle.CSS">
    <script type = "text/javascript">        
       var x=0;
       function changeImage() 
       {
if (x == images.length){
return;      
}

          document.getElementById("img").src=images[x]
          x++;
       }


      var images =  ["S2b.jpg", "S3b.jpg", "S4b.jpg", "S5b.jpg", "S1b.jpg"];       

    </script>
</head>
<body>
    <div id="MemImg">
       <button onclick="changeImage()" >
           <img id="img"  width="700px" height="700px" >
       </button>   
     </div>
</body>

Answer №1

Here is a solution for you to try:

let index = 1;
function switchImage() 
{
    if (index > pictures.length)
    {
        return;
    }

    document.getElementById("photo").src = pictures[index];
    index++;
}

Also, as suggested by @johnsmith, you can initialize the array of images like this:

const pictures = ["pic2.jpg", "pic3.jpg", "pic4.jpg", "pic5.jpg", "pic1.jpg"];

Update your index variable to start at 0:

let index = 0;

And adjust the condition accordingly:

if (index == pictures.length)

If you want to reset the counter, follow @janedoe's advice and change return; to index = 0.

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

What is the best way to have a variable adjust each time a coin is inserted until it reaches a specific value?

I have developed a unique coin box that recognizes the value of each coin inserted. Users can now pay for a service that costs 2.0 € by inserting coins of various denominations such as 2.0 €, 1.0 €, 0.50 €, 0.20 €, and 0.10 €. In my react-nati ...

Obtain the sender using an href JavaScript code

Is there a way to retrieve the tag that called a javascript function if the anchor has a href tag of javascript:someFunc()? I know that in the onclick attribute, you can pass this, but when called from the href tag, this references the DOMWindow. Unfortu ...

What is the best way to handle waiting for an API call in JavaScript export?

In my Vue app, I've set up my firestore app initialization code as shown below: if (firebase.apps.length) { firebase.app() } else { firebase.initializeApp(config) } export const Firestore = firebase.firestore() export const Auth = firebase.auth() ...

What is the best way to adjust the size of a canvas and then upload the resized information to a

I need to update the user's database table row data with new resized dimensions (changing width and height). For instance: In my online editor, I create a canvas of size 1000x500, add objects, and save -> This data is sent to the database When re ...

Passing data from an Express middleware to a Jade template

My KeystoneJS app is all set up, using Express and Jade. The default.jade file sets up a fullscreen background image along with various imports, the header, and footer of the site. I am attempting to rotate the image based on a selection of images stored ...

Facing issues with building Angular 7 and Ionic 4 - getting error message "TypeError: Cannot read properties of undefined (reading 'kind')"

I need assistance with a problem I am encountering while building my Angular 7 & Ionic 4 application... When I run the ng build --prod command, I encounter the following error: ERROR in ./node_modules/ionic4-auto-complete/fesm5/ionic4-auto-complete.js ...

Guide on extracting JSON data from specific nodes using d3.js

Just starting out with d3.js and following the example of a simple molecule created by d3 at http://bl.ocks.org/mbostock/3037015. I have a couple questions: 1. How can I select multiple nodes within the molecule structure? 2. Once selected, how do I ext ...

Identify Numerous Unique HTML-5 Attributes within a Single Page - Adobe DTM

Is there a way to automatically detect and aggregate multiple custom HTML-5 attributes, such as "data-analytics-exp-name," into a cookie using Adobe DTM without user interaction? These attributes would only need to exist on the page and not require any cli ...

Obtain a null value for the hidden field in ASP from a JavaScript parameter

Trying to transfer the selected parameter value from a dropdown menu to a hidden field but the hidden field always ends up empty. No errors in the JavaScript code when traced using browser debugger. What could be the issue? JavaScript $(document).ready( ...

The function 'ShouldWorkController' was expected but is not defined, receiving undefined instead

Whenever I attempt to connect a controller to a template using the angular-ui-router $stateProvider, I encounter this error message: 'ShouldWorkController' is not a function. Got undefined. Surprisingly, when I define the controller within the ...

Adjusting the color of the legend on a LineChart in ExtJS 4 on-the-fly

I've been trying to find information on how to modify the color of the x legend in a Line chart without success. Can anyone help me with this? I have included an image of the chart for reference. ...

The function forEach is unable to handle the process of uploading multiple images to cloudinary

I'm facing an issue with uploading multiple images to Cloudinary from my Vue2JS front-end. I have successfully created a function that uploads a single image, but I am struggling with uploading multiple images using a forEach loop. upload(evt) { ...

Does Transclude eliminate tr and td elements from the content?

I'm encountering an issue with my angularjs application where the tr and td tags are mysteriously disappearing. angular.module('transcludeExample', []) .directive('pane', function() { return { restrict: 'E' ...

If you're not utilizing v-model.lazy, Vue3 Cleave js may encounter functionality issues

I am currently experimenting with cleavejs to format the thousand separator in my input numbers. I've noticed a strange behavior where if I input 1000.123, it displays as 1,000.12 which is the correct format. However, the v-model value remains as 1000 ...

What steps should I follow to bring in an animated SVG file into Next.js 13 without losing transparency and animation effects?

How to Include an Animated SVG File in Next.js 13 import Image from "next/image"; export default function Home() { return ( <main className="flex h-screen flex-col items-center"> <div className="container mt-1 ...

Choose All Box for Dynamic Tables in AngularJS

Hi everyone, I'm currently working on adding a select-all checkbox to the top of my list of checkboxes using a custom directive. I found some guidance on how to do this in a thread that I came across: https://github.com/lorenzofox3/Smart-Table/issues/ ...

Is there a way to retrieve a different value within the JavaScript code (imagepicker)?

I need help with setting up a page where users can choose an image to forward them to another page. However, I'm facing an issue when the user chooses two images at once. Can anyone provide ideas on how to handle forwarding in this scenario? You can c ...

Exploration of features through leaflet interaction

Attempting to plot bus routes on a map using leaflet with geojson for coordinates. Facing issues with making the bus line bold when clicked, and reverting the style of previously clicked features back to default. Current Progress function $onEachFeature( ...

Tips for dynamically modifying style mode in Vue.js

I am looking to implement a feature where the style can be changed upon clicking a button. In my scenario, I am using Sass/SCSS for styling. For example, I have three different styles: default.scss, dark.scss, and system.scss. The code for dark.scss look ...

Undo a CSS transition when clicked

There is a div named learn-more which expands to fill the whole page when a CSS class is added like this: .learn-more{ padding:10px; font-size: 20px; text-align: center; margin-top:20px; font-family: klight; -webkit-transition:2s; ...