I created an image that can be clicked on, but unfortunately it only functions properly on the

I am currently working on creating an image that can be clicked to cycle through different images all within the same frame. While I have managed to get it to work, I am facing a limitation where it only responds to one click.

count = 1;

function myFunction(a, b, c) {
  if (count === 1) {
    document.getElementById("johnnycashpicscroll").src = b;
  }
  if (count === 2) {
    document.getElementById("johnnycashpicscroll").src = c;
  }
  if (count === 3) {
    document.getElementById("johnnycashpicscroll").src = a;
  }
  if (count === 3) {
    count = 1;
  }
}
h1 {
  font-size: 50px;
  font-family: sans-serif;
  position: relative;
  bottom: 130px;
  left: 120px;
}

#topleftimg {
  width: 100px;
  height: 120px;
  top: 100px;
}

hr {
  position: relative;
  bottom: 115px;
}

button {
  margin-left: 20px;
  margin-right: 20px;
  width: 120px;
  height: 60px;
  font-family: sans-serif;
  font-size: 19.5px;
  font-weight: bold;
}

#button1 {
  margin-left: 50px;
}
<!DOCTYPE html>

<html>

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title></title>
  <meta name="description" content="">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="">
</head>

<body>
  <img id="topleftimg" src="https://th.bing.com/th/id/OIP.uSeMc9l3nH-tNFb-N2KrrgAAAA?w=155&h=195&c=7&r=0&o=5&pid=1.7" alt="Johnny Cash">
  <h1>Johnny Cash<button id="button1">Home</button><button>Childhood</button><button>Career</button><button>Challenges</button><button>Legacy</button></h1>
  <hr />
  <img src="JohnnyCashPic1.jfif" alt="" height="155" width="100" id="johnnycashpicscroll" onclick='myFunction("JohnnyCashPic2.jfif", "JohnnyCashPic3.jfif", "JohnnyCashPic1.jfif")'>
</body>

</html>

If you're wondering, I'm developing a Johnny Cash informational website for a school project.

Answer №1

For proper comparison in your function, make sure to use == or === instead of =.

let count = 0;
const images = ["JohnnyCashPic1.jfif", "JohnnyCashPic2.jfif", "JohnnyCashPic3.jfif"];
function myFunction(){
  const imgElement = document.getElementById("johnnycashpicscroll");
  count = (count % images.length) + 1;
  imgElement.src = images[count - 1];
}
<style>
        h1 {
        font-size: 50px;            
        font-family: sans-serif;
        position: relative;
        bottom: 130px;
        left: 120px;
        }
        #topleftimg {
        width: 100px;
        height: 120px;
        top: 100px;
        }
        hr{
        position:relative;
        bottom: 115px;
        }
        button {
        margin-left: 20px;
        margin-right: 20px;
        width:120px;
        height:60px;
        font-family:sans-serif;
        font-size:19.5px;
        font-weight: bold;
        }
        #button1 {
        margin-left: 50px;
        }
    </style>
<!DOCTYPE html>

<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title></title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="">
    </head>
    <body>
        <img id="topleftimg" src="https://th.bing.com/th/id/OIP.uSeMc9l3nH-tNFb-N2KrrgAAAA?w=155&h=195&c=7&r=0&o=5&pid=1.7" alt="Johnny Cash">
        <h1>Johnny Cash<button id="button1">Home</button><button>Childhood</button><button>Career</button><button>Challenges</button><button>Legacy</button></h1>
        <hr />
        <img src="JohnnyCashPic1.jfif" alt="" height="155" width="100" id="johnnycashpicscroll" onclick='myFunction()'>
    </body>
    
</html>

The code above should be functioning correctly now. Ensure all image sources are valid for the code to work as expected.

Edit: I appreciate the feedback and have made necessary corrections to improve the code functionality. Instead of passing individual image names to the function, I have utilized an array within the function to cycle through images indefinitely.

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

php code to show data in a single column of a table

I am working with a database record that includes an id and name, and I need to display it in a table where records are distributed in the pattern 1-2-1-2-4 across columns. Below is my current code that is generating the incorrect output: <table borde ...

The keyframe spinner fails to function properly on Firefox and results in blurry text

I am facing an issue with a keyframe spinner that rotates an image on the y-axis. It works perfectly in Chrome but fails to function in Firefox. I have tried changing the HTML tags to div or span class/id, but nothing seems to work. Interestingly, other ke ...

The TinyMCE extension in Yii fails to load CSS files when accessed through a subdomain

My Yii application located at site.com/module has the tinymce extension installed, but I am facing an issue where the tinymce CSS files are not loading when I access the application through the sub-domain alias module.site.com. Interestingly, all JS file ...

Secure User Validation Using ExpressJS and CouchDB

I am currently working on implementing a CouchDB User login into my express app using a frontend form and storing the login information in the session. Here is what I have done so far: In my app.js file: var express = require('express'); var co ...

Is it possible to display the HTML video poster at the end of the video without relying on jQuery or JavaScript?

I have a webpage with multiple informational videos, using basic HTML/CSS player markup without any Javascript/jQuery knowledge. After a video is played or paused, the poster image does not reappear. Is there a way to recall the poster within the simple HT ...

Internet Explorer does not return results when using AJAX during the onchange event (specifically for IE only

My code is functioning correctly on other browsers, however in IE it does not provide any result when I select the dropdown button. Instead, it changes and displays an empty result. This is my AJAX: $("#book").change(function(){ var DOMBULK = $(" ...

Is there a way to right-align the labels and input fields simultaneously?

Is there a way to line up the labels and inputs on the right side so they all appear in the same row? .radioContainer{ width: fit-content; margin: 5px; margin-top: 20px; background-color: aqua; padding-bottom: 25px; } <div class=" ...

What are some methods for utilizing the data received through this.props.history?

Whenever I need to navigate from one route to another using this.props.history.push("/"), I also want to pass along some extra data. For example: this.props.history.push('/postDetail', {data: item}). However, I am uncertain about where to define ...

The functions firebase.auth().currentUser and onAuthStateChanged consistently return null even when the user is logged in

Despite the fact that my front end Swift application indicates that the user is signed in, I am encountering an issue where firebase.auth().currentUser and the onAuthStateChanged listener in my node.js code return null. The backend service is called from t ...

What benefits and drawbacks come with setting up JS libraries in resource files compared to package.json?

Configurations for JavaScript libraries such as Babel, Nyc, Eslint, and many others can be specified in resource files or within the package.json. For example, Babel can be set up in a .babelrc file or through a babel entry in the package.json. What are ...

How to find a collision between a spherical object and a triangular shape in a three

In my research, I am exploring the possibility of detecting collisions between a triangle and a sphere in three.js. Currently, I have devised a method using the raycaster and the sphere's vertices. However, this approach has proven to be unreliable a ...

Implementing promise-based looping for multiple GET requests in node.js

As a newcomer to promises, I've been searching for the right answer or pattern without much luck. Currently using node.js v4.2.4 and exploring The task seems simple enough... I need to execute multiple asynchronous blocks in a specific order, with on ...

Chosen option from the Bootstrap drop-down menu

Struggling with two problematic input fields on my Bootstrap website... The first is a Bootstrap Typeahead input field: <input type="text" id="typeahead" name='name' placeholder='The name' class="form-control" data-provide="typeahe ...

Link a PHP variable to a JavaScript variable

Is there a way to set the value of a JavaScript variable using a PHP variable? $(function(){ $("select[name=myselectlist]").change(function(){ var id = $(this).val(); if(id != 0) { $.post("ajax.php", {"id":id}, func ...

Display the number of rows per page on the pagination system

Looking for a way to add a show per page dropdown button to my existing pagination code from Mui. Here's the snippet: <Pagination style={{ marginLeft: "auto", marginTop: 20, display: "inline-b ...

Attempting to create a button that will only appear for items with a defined value for a specific variable

I am currently facing an issue with a template that needs proper population. The store locator on my website lists pharmacies and displays their relevant information (phone number, address, hours of operation) along with three buttons (view store, view fl ...

Whenever I try to make my links responsive, they don't seem to work as intended

This is the HTML snippet <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> ...

Using PHPMailer to send attachments uploaded through a form

I have a HTML form where users can select an attachment file (such as a PDF) and I want to send that file via email to a specified destination. While I am able to successfully send all other inputs through email, I'm having trouble with the attachmen ...

Switch button while moving cursor

I'm struggling with getting this 2048x512 image, which has 4 stages of transition, to work properly. https://i.sstatic.net/1PBvX.png While I know how to switch it to the final stage on hover, I can't seem to figure out how to incorporate a trans ...

Consolidate list: Make sure to leave only the currently active item open

For some reason, I am encountering an issue with this code on this platform. The problem is that every time I click on a list title, all items open up instead of just the one I clicked on. Is there a way to modify this code so that only the clicked item ex ...