When employing the onClick event in JavaScript, the function is executed only a single time

I want a div's background image (#reaction-background) to change randomly every time a button is clicked (#angry) using onclick event. However, currently the background image only changes once after clicking the button.

HTML:

<div class="btn-list">
      <a id="angry">ANGRY</a>
      <div id="reaction-background"></div>

Javascript:

// array of different images for reactions
var fileNamesReactions = [
  "angry1.jpg",
  "angry2.jpg",
  "angry3.jpg",
  "angry4.jpg",
  "angry5.jpg"
];
// get a random index from the array 
var randomIndexReaction = Math.floor(Math.random() * fileNamesReactions.length);
// function to change background image on button click
document.getElementById("angry").onclick = function() {
  document.getElementById("reaction-background").style.background = "url(./img/reactions/angry/" + fileNamesReactions[randomIndexReaction] + ")";
  document.getElementById("reaction-background").style.backgroundRepeat = "no-repeat";
  document.getElementById("reaction-background").style.backgroundSize = "contain";
}

Answer №1

Each time you click, your click event handler runs. However, the issue lies in how you only generate a random number once, before any clicks occur. This results in the same background image being set repeatedly upon further clicks.

To resolve this, move the random number generator inside the click callback so that a new random number is generated with each click.

Avoid using the onclick property of the element to set up the callback. Instead, opt for the more modern and reliable .addEventListener() method for setting up events.

Furthermore, try to handle styling in CSS whenever possible and utilize CSS classes.

Here is an example integrating all these points:

// array of pictures
var fileNamesReactions = [
  "angry1.jpg",
  "angry2.jpg",
  "angry3.jpg",
  "angry4.jpg",
  "angry5.jpg"
];

// Get your DOM references that you'll use repeatedly just once:
let backgroundElement = document.getElementById("reaction-background"); 

// randomize pictures
document.getElementById("angry").addEventListener("click", function() {
  // You have to get a new random each time the click occurs.
  var random = Math.floor(Math.random() * fileNamesReactions.length);
  backgroundElement.style.background = "url(./img/reactions/angry/" + fileNamesReactions[random] + ")";
  console.log(backgroundElement.style.background);
});
/* Use CSS classes as much as possible as they make code much simpler */
#reaction-background {
  background-size:"contain";
  background-repeat:"no-repeat";
}
<div class="btn-list">
      <a id="angry">ANGRY</a>
      <div id="reaction-background"></div>
</div>

Answer №2

Make sure to move the random number statement inside the onclick method so that a new number is generated every time the user clicks. You can refer to the code snippet below:

// array of pictures
var fileNamesReactions = [
  "happy1.jpg",
  "happy2.jpg",
  "happy3.jpg",
  "happy4.jpg",
  "happy5.jpg"
];

// shuffle pictures randomly
document.getElementById("happy").onclick = function() {

// get a random index for reactions
var randomIndexReaction = Math.floor(Math.random() * fileNamesReactions.length);

  document.getElementById("reaction-background").style.background = "url(./img/reactions/happy/" + fileNamesReactions[randomIndexReaction] + ")";
  document.getElementById("reaction-background").style.backgroundRepeat = "no-repeat";
  document.getElementById("reaction-background").style.backgroundSize = "contain";
}

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

The mysterious anomaly in Vue.js

I am attempting to assign a data object named types upon receiving a response in the ready() method. This is what I have: export default { data () { return { types: null } }, ready () { TypeService.showAll(1) .then(functio ...

Differences between an AngularJS function() and a factory function() when used in a

When it comes to Angular, I've come across directives being written in two different ways: .directive('example', function () { // Implementation }); .directive('example', function factory() { // Implementation }) Can you ...

Utilizing Google Maps to display an infowindow upon clicking a location from a geojson file

I want to implement info windows that pop up when markers on a map are clicked. I used the sample code provided by Google (below) to create a marker map from geojson files dragged and dropped into the window. My goal is for the info windows to show text ta ...

Changing a base64 string to an image within an angular 4 environment

My current task involves cropping an image using ng2-image cropper, but the result is in base64 format. I need to convert it to an image source so that I can send it to a server. Despite searching, I haven't been able to find anything compatible with ...

Reorganizing divisions with Dojo

I came across a thread on StackOverflow that was similar to what I'm looking for. jQuery removing an element and renumbering remaining elements However, since we don't use jQuery but instead rely on Dojo, I'm unsure how to achieve the same ...

How can JavaScript be used to create a unique signup and login process on

I am struggling with storing sign up and login details in JavaScript. In my previous project, I used PHP and a database to handle this information, so transitioning to JavaScript has been challenging. Here is an example of the sign-up HTML code: <!DOC ...

Using Selenium to choose a dropdown menu option - the dropdown menu will only appear once a search query has been entered into the

Seeking assistance with choosing a pop-up dropdown menu option using selenium in Python within Google Colab. You can find the URL here: This website allows searching by geography. Once you enter a term in the Domain field (I'm using 'Cerrado&ap ...

Enabling tab functionality through navigation and applying a class to the selected navigation button

Having a navigation bar with tabs that open corresponding content on a page when clicked. The tab selection on the page is working fine - content shows and highlight changes. However, the issue arises when using the navigation bar as it changes the content ...

ERROR: The Search function is not defined and cannot be accessed when the HTMLInputElement event is triggered

I am working on an app that utilizes Marvel's API, and I've been having trouble implementing a search bar in the application. Whenever I attempt to search for a name within this API, the function Search() is showing up as undefined in the HTML. ...

Execute CGI upon the loading of the page

My goal is to achieve the following: When the HTML page loads, I want to execute a CGI script written in C to call a specific function. My current approach involves using a JavaScript function in the HTML: <body onload="MyJsFunc();"> Then, in th ...

Element that appears to be hidden

Looking to add a unique touch with a "chat bubble" design on my webpage. Below is the code I have so far, but I seem to be facing issues with getting the triangle shape to display. Any suggestions on what might be causing this problem? .playernamechat ...

What is the best method for inserting dynamic HTML content (DIV) with JavaScript?

Can someone assist me in dynamically adding HTML content when data is returned from the server-side? I am currently using ajax/jQuery to handle server-side processing requirements. The success code section of my ajax function allows me to write HTML elemen ...

Making multiple Ajax calls can lead to receiving duplicate responses

There seems to be an issue with my ajax function that retrieves the Price of items from my DB. Although the php code appears fine, I sometimes receive duplicate results! Using jQuery $('.removemore___').click(function(e) { var item_id = $(thi ...

Transfer the position of the export button in the Datatable to the lower section of

I have customized an export feature in my datatable to display below the table instead of above it. Specifically, I want the download button to appear at the end of the table next to the pagination controls. $(document).ready(function (){ var table = ...

Personalized Element Commands and features

UniquePage.html <div ng-controller="UniquePageCtrl"> <unique-custom-directive arg1="{{currentObj.name}}"></my-custom-directive> <div> in UniquePageCtrl.js (Controller) app.controller("UniquePageCtrl", ["$scope", function ($sc ...

Mapping the Way: Innovative Controls for Navigation

Currently, I am utilizing the HERE maps API for JavaScript. However, I would like to customize the design of the map controls similar to this: Below is an example for reference: HERE EXAMPLE Is it feasible to achieve this customization? ...

Modules controllers angular.js namespacing

In my application directory, I have the following structure: scripts/ modules/ module1/ controllers/ MainController.js module2/ controllers/ MainController.js main.js I am looking to organize the controll ...

Positioning bar chart columns and text using CSS

My component generates HTML for a simple bar chart, with the height and background color computed within the component. Everything functions correctly, but I am facing an issue where long text causes displacement of the bar above it. Another problem is th ...

What is the best way to create a video that takes up the entire height of a half-sized

page url: I am facing a challenge with displaying a video that has dimensions of 4096x2160 pixels. I want to show the centered part of the video with full height on my left half screen. Despite trying various methods, I have not been able to find a suita ...

The act of selecting a parent element appears to trigger the selection of its child elements as well

I am attempting to create an accordion using Vanilla JavaScript, but I have encountered an issue. When there is a child div element inside the header of the accordion, it does not seem to work and I'm unsure why. However, if there is no child div elem ...