The functionality of the onclick event for images in motion is not functioning properly

I am attempting to track the number of clicks on the fox image, but for some reason it's not working as expected. I have confirmed that the onclick function is functioning properly.

$(document).ready(function () {
    var clickCount = 0;
    $(".fox").click(function(){
        $(".animal-quantity").html(clickCount++);
    });
});

If anyone can offer assistance with this issue, I would greatly appreciate it. Thank you in advance. You can view my full code at the following link: https://jsfiddle.net/rbtj3ywo/1/

Answer №1

Within your CSS stylesheet, there is the following code:

#animals {
    pointer-events: none;
}

Important Note: The property pointer-events: none disables any interaction with the element.

To resolve the issue, simply remove this code from your stylesheet.

Answer №2

Your CSS rule pointer-events: none; is not correct.

$(document).ready(function () {
var i = 0;
$(".fox").on('click',function(){
   console.log("hi");
   $("#quantity > span").text(i++);
});
});

(function() {
        var NUMBER_OF_ANIMALS = 10;

        function initialize() {
            /* Get a reference to the element that will contain the animals */
            var container = document.getElementById('animals');

            /* Fill the empty container with new animals */
            try {
                for (var i = 0; i < NUMBER_OF_ANIMALS; i++) {
                    container.appendChild(createAnimal());
                }
            }
            catch(e) {
            }
        }

        function randomInt(low, high) {
            return low + Math.floor(Math.random() * (high - low));
        }

        function randomFlt(low, high) {
            return low + Math.random() * (high - low);
        }

        function pixelVal(val) {
            return val + 'px';
        }

        function durationVal(val) {
            return val + 's';
        }

        /*
         Uses an img element to create each animal. 
         */
        function createAnimal() {
            /* Start by creating a wrapper div, and an empty img element */
            var animalDiv = document.createElement('div');
            var image = document.createElement('img');

            /* Randomly choose an animal image and assign it to the newly created element */
            image.src ='https://pngimage.net/wp-content/uploads/2018/05/cute-animals-png-4.png';
        
            image.style.width = randomInt(7, 20) + 'vw';
            image.className = 'fox';

            /* Position the animal at a random location along the screen */
            animalDiv.style.top = pixelVal(randomInt(-100, -250));
            animalDiv.style.left = randomInt(0, 60) + 'vw';

            /* Set the -webkit-animation-name property with these values */
            animalDiv.style.webkitAnimationName ='fade, drop';
            animalDiv.style.animationName ='fade, drop';

            var fadeAndDropDuration = durationVal(randomFlt(1.2, 8.2));
            animalDiv.style.webkitAnimationDuration = fadeAndDropDuration + ', ' + fadeAndDropDuration;
            animalDiv.style.animationDuration = fadeAndDropDuration + ', ' + fadeAndDropDuration;

            var animalDelay = durationVal(randomFlt(0, 1));
            animalDiv.style.webkitAnimationDelay = animalDelay + ', ' + animalDelay;
            animalDiv.style.animationDelay = animalDelay + ', ' + animalDelay;
            
            animalDiv.appendChild(image);
            return animalDiv;
        }
        initialize();
    }
)();
.fox {
  z-index: 100;
}

#animals {
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            width: 100%;
            z-index: 98;
        }
        #animals > div {
            position: relative;
            -webkit-animation-iteration-count: infinite;
            -webkit-animation-direction: normal, normal;
            -webkit-animation-timing-function: linear, ease-in;
            -webkit-backface-visibility: hidden;
            animation-iteration-count: infinite;
            animation-direction: normal, normal;
            animation-timing-function: linear, ease-in;
            backface-visibility: hidden;
        }
        #animals > div > img {
            -webkit-animation-iteration-count: infinite;
            -webkit-animation-direction: alternate;
            -webkit-animation-timing-function: linear;
            -webkit-backface-visibility: hidden;
            animation-iteration-count: infinite;
            animation-direction: alternate;
            animation-timing-function: linear;
        }
        @-webkit-keyframes fade {
            0%, 90% {
                opacity: 1;
            }
            100% {
                opacity: 0;
            }
        }
        @keyframes fade {
            0%, 90% {
                opacity: 1;
            }
            100% {
                opacity: 0;
            }
        }
        @-webkit-keyframes drop {
            0% {
                -webkit-transform: translate3d(0, 0, 0);
            }
            100% {
                -webkit-transform: translate3d(0, 1100px, 0);
            }
        }
        @keyframes drop {
            0% {
                transform: translate3d(0, 0, 0);
            }
            100% {
                transform: translate3d(0, 1100px, 0);
            }
        }
        .quantity{
            position: absolute;
            left: 0;
            right: 0;
            text-align: center;
            top: 100px;
        }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="animals"></div>
        <img src="https://img.rawpixel.com/s3fs-private/rawpixel_images/website_content/pf-misctexture01-beer-000_5.jpg?w=800&dpr=1&fit=default&crop=default&q=65&vib=3&con=3&usm=15&bg=F4F4F3&ixlib=js-2.2.1&s=c1552a7bdc2ea7b6e17d8d0d893c15be" class="background" style="width: 100%; position: relative;">
<div class="quantity" id="quantity">Total quantity: <span class="animal-quantity">0</span></div>

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

Tips on preventing jquery ui tooltip from appearing within a child div

Is there a way to include a tooltip only in the parent div and not in its child div? I am currently using jQuery UI tooltip like this: $(function () { $("#parent_div").tooltip(); }); This is how the HTML looks: <div id="parent_div" title="Hello ...

Aligning elements in the Bootstrap 3 Grid

Hey there, fellow brainiacs! I'm in need of some assistance with aligning my bootstrap grid. The issue I'm facing is that it doesn't stack properly when in responsive mode. I would really appreciate some guidance on how to make it fold and ...

Is your prop callback failing to return a value?

I am currently utilizing a Material UI Table component in my ReactJS project and I would like to update a state variable whenever a row is selected or deselected. The Table component has an onRowSelection prop that gets triggered each time a row is is sele ...

Can you confirm if Jquery's $.POST method works asynchronously?

I have been attempting to create a div on my index page that, when clicked, triggers a JavaScript function to send data to a PHP page for computations and MySQL queries. However, despite setting an onclick event handler in my main PHP page, the Chrome deve ...

Regular expressions in Python do not function properly on text data

In my HTML file, I am using lxml and BeautifulSoup to convert it to text. However, I am encountering a problem with ill-formed HTML that I want to remove. I attempted to use a regular expression to match the problematic string, but it did not work as expec ...

Tips for stopping .php page refreshing following an .ajax request

On my mathProblems.php page, I have set up a system to generate math problems for users. When the user submits their answers using a form with id='math_answers', an ajax call is made to grade the math questions and display the results. The gradi ...

Creating a Customized Pop-up Box with Bootstrap CSS and JQuery/Java Integration

I have a filter box that appears when the filter icon is clicked, but it currently lacks Bootstrap styling. I am in the process of upgrading the website to Bootstrap3 HTML5. The current appearance of the filter icon is as follows: However, we want it to ...

Adding flair to the selected element in the HTML nav bar

I was attempting to customize the appearance of the active item within an HTML navigation bar, which corresponds to the a element. To test this out, I used the example from Here is my modified code, where I introduced a new class "active" and applied the ...

Error in TMDb API: JSON data abruptly ended

Currently, I am attempting to parse JSON data that is being returned by The Movie DB. Unfortunately, I keep encountering an error message indicating the following: Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) ...

Enhance your navbar with interactive image effects on hover

Looking to create a CSS menu that changes images and background when hovered over. Below is the code for my menu: HTML <ul> <li class="active1"> <a href="profile.html" class="act">My Profile</a></li> <li><a ...

Is it considered good practice to utilize Vue.js for managing global shared state and implementing for loops outside of components?

Just getting started with Vue.JS and experimenting with creating two lists of books (read and unread) from a shared object in the global data state. This is my approach - working demo (works like a charm! However, I'm wondering if this is considered ...

Creating a boundary: A step-by-step guide

There is an element that I need help with <div id="square"></div> This element has the ability to move around the document var square = document.getElementById("square"); document.body.onkeydown = function(e) { if (e.keyCode == 37) { ...

What is the best way to transform an array of lists into a neatly organized state?

I recently received a list of breweries from an API call and I am trying to format it into my React state container. Here is what I have so far: state = { breweries: [ {name: Foo, long: 45, lat: -39.239}, ...

Develop a specialized WordPress widget featuring several unique text areas for enhanced customization

class ad_widget extends WP_Widget { function __construct() { $widget_ops = array( 'classname' => 'ad-widget-container', 'description' => __( ' Ad WIDGET' ) ); parent::__construct( 'ad-widget', ...

Is it possible to update only the inner text of all elements throughout a webpage?

Scenario After coming across a thought-provoking XKCD comic, I decided to craft a script that would bring the comic's concept to life: javascript:var a=document.getElementsByTagName('body')[0].innerHTML;a=a.replace(/Program(\w\w+ ...

Fade out one div and then fade in a new one using Jquery

Hey there! I'm looking to create a smooth transition effect for my website where the content fades out when clicking on a menu item, and then fades in with the new content based on the link provided. Here's an example: . So basically, when I clic ...

Where should the JQuery hashchange event be added for optimal placement?

I am currently utilizing the JQuery hashchange event. $(window).on('hashchange', function () { //perform certain actions }); On the initial load, if my URL includes a hash value, I know that it must be triggered by $(window).hashchange(); Is i ...

What is the best way to handle constants in TypeScript?

I am facing an issue with a React component I have created: const myComponent = ({constant}: Iprops) => ( <div> {CONSTANTS[constant].property ? <showThis /> : null </div> ) The error message says 'element implicitly has ...

Transforming date and timezone offset into an isoDate format using moment.js

When retrieving data from the API, I encounter Date, Time, and Offset values in separate columns. My goal is to obtain an ISO date while maintaining the original date and time values. const date = "2019-04-15" const time = "13:45" const ...

My jQuery code seems to be in order, so why isn't it working as expected?

I've been attempting to create basic jQuery play/pause buttons for an HTML5 video, but when I click on them, nothing seems to happen. If you have any code that could help with the play/pause functionality, I would greatly appreciate it. Here is the c ...