Tips to troubleshoot problem with Javascript ``addEventListener`` ``click`` not functioning

I've been attempting to incorporate the "addEventListener" feature into my code. Despite following an example, I can't seem to get it to work. Is there something I'm missing or doing wrong?

<body>
    <div class="emoji-row ng-scope" ng-repeat="...">
        <div class="emoji-wrapper">
            <div class="emoji-reactions">
                <div class="emoji-reactions-main">
                    :)
                </div>
                <div class="emoji-reactions-panel">
                    <ul>
                        <li> :) </li>
                        <li> :( </li>
                    </ul>
                </div>
            </div>
        </div>
        <span class="helloemoji"></span>
    </div>
    <script>
        const tag = document.createElement("span");
        tag.className = 'helloemoji';
        document.getElementsByClassName("emoji-row")[0].appendChild(tag);

        const emojis = document.getElementsByClassName("emoji-reactions-panel")[0].getElementsByTagName("li");
        console.log(emojis.length); /* 5 */

        Array.from(emojis).forEach(function (item) {
            console.log("item", item);
            item.addEventListener('click', emojiClickfunc);
        });

        function emojiClickfunc() {
            console.log("clicked...")
            alert('clicked...');
        }
    </script>

    <!-- style -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</body>

I even created a sample example, which seems to work fine there! But for some reason, not within my own code as shown above :(

Any advice on how to rectify this issue would be greatly appreciated.

EDIT:

In my attempts to troubleshoot, I added an event listener to the window


        Array.from(emojis).forEach(function (item) {
            console.log("item", item);
            window.addEventListener('click', emojiClickfunc);
        });

I'm still struggling to resolve this. I need to determine which line in an unordered list was clicked by the user, rather than through a window event. Any ideas?

Answer №1

Although I am not familiar with AngularJS, if you are encountering a problem related to it, perhaps exploring solutions within the AngularJS framework could be beneficial.

One suggestion is to consider utilizing the ng-click directive in AngularJS, as outlined in the documentation: https://docs.angularjs.org/api/ng/directive/ngClick

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

Accessing Route Parameters from Any Component

If my URL follows this structure: /blog/[post_id]/something What's the best practice for passing $post_id to any component within the tree? I'm familiar with retrieving route parameters using getInitialProps, but I struggle with passing values ...

Effortlessly browse through directory with the seamless integration of AngularJS or

Hey there! I'm working on a really cool feature - creating a list with editable inputs. However, I've encountered a bit of an issue. Is there any way to navigate through this list using arrow keys and focus on the desired input? .content ul { ...

angularJS controller does not seem to be functioning properly due to a certain

var today = new Date(); var dd = today.getDate(); var mm = today.getMonth() + 1; //January is 0! var yyyy = today.getFullYear(); var currentdate = yyyy + ',' + mm + ',' + dd; var appointmentDate = moment($scope.AppointmentsList[i].J).f ...

As my character slides off the moving platform in this exciting Javascript canvas game, my heart

Can anyone help me figure out how to keep my player on the moving platform? I'm not sure if I need to add gravity or something else. I'm still learning the ropes. export function checkTopCollision({ item1, item2 }) { return ( item1.y + item ...

Exploring Various Shapes in THREE JS

Currently, I am utilizing the EdgesGeometry for rendering my objects. I am currently exploring options on how to morph a Tetrahedron into an Octahedron, an Octahedron into a Box, a Box into a Sphere, and vice versa. Despite searching through numerous doc ...

The function setAttribute does not work with arrays

I'm facing an issue where I need to conditionally assign an attribute to each item in an array based on a radio button value. I have been able to set the required attribute on individual items, but I'm struggling to apply it to all elements in th ...

What steps can I take to enable search functionality in Ckeditor's richcombo similar to the regular search feature in

I am currently developing a custom dropdown menu using the rich combo feature in CKEDITOR. One of my goals is to include a search functionality within the dropdown, such as a key press search or an input textbox search. This is how my dropdown box appear ...

Utilizing Jquery to locate a link inside a Div and displaying it as specified in the HTML

Looking for a solution to make div elements clickable as links? The usual methods involving window.location or window.open may not be suitable if you have numerous divs with predefined targets. If you're not well-versed in Javascript, finding the righ ...

What is the most effective method for incorporating keyframes using JavaScript in a dynamic way?

Is there a way to animate an HTML element by using a variable with keyframes, but without directly manipulating the DOM? Below is the HTML code: <div class="pawn" id="pawn1"></div> Here is the CSS keyframes code: @keyframe ...

In the ajax call, an empty JSON array object is sent as the data

Utilizing JSON data as a parameter for an ajax call: var startDate = dateFormatForSave($("#start_date").val().trim()); var arrayOfStudentsInfo = []; var table = $("#selected_students"); table.find('tr').each(function(i, el) { var rowId = $( ...

Easiest homemade variations and pairings

Imagine having a basic array like ["apple", "banana", "lemon", "mango"];. For example, if we were to find the straightforward hand-rolled combinations of this array, selecting 3 items, but allowing for repetition: let array = ["apple", "banana", "lemon", ...

Search through a JSON array to find a specific element and retrieve the entire array linked to that element

Recently, I've been working with a json array that dynamically increases based on user input. Here's a snippet of the json code I'm dealing with: [{"scheduleid":"randomid","datestart":"2020-06-30",&quo ...

Maintain the modifications in NPM software dependencies

Currently, I am developing a REACT web application and have integrated the react-datasheet library using NPM. To ensure compatibility with IE11, I made modifications to the JavaScript file installed via NPM. While these changes work perfectly on my local m ...

Utilizing doT.js for Organizing Nested Lists from Arrays and Objects

Can nested lists be generated with doT.js? My current code only processes the first object in the array (g1) and ignores the rest. Is there a solution for this using doT.js? The desired result should be: G1 T11 T12 T13 G2 T21 T22 T23 $(document) ...

Function starting too slow due to rapid Loading Spinner Image display

I am struggling with a function that is supposed to display the contents of posts when clicked on. My goal is to have a loading spinner appear for a few seconds before the post content shows up. However, I am facing an issue where the spinner only appears ...

The $state.go function does not function as anticipated during the $stateChangeStart event

When using $state.go with $stateChangeStart and logging out, I noticed that $state.go only changes the URL without loading the template. However, this issue does not occur if I use $location.path('/signin'). Interestingly, this behavior only happ ...

Making an ajax request to update the value of a specific key in an object

I am currently working with a firebase database that contains multiple collections. Using the code provided, I have managed to successfully retrieve an array that consists of all attractions with an area_id that matches the area_id of e.target. However, t ...

Encountering NaN while trying to retrieve the duration in JavaScript

I'm having an issue retrieving the duration of an mp4 video file when the HTML document loads. Here's my code: (function ($, root, undefined) { $(function () { 'use strict'; $(document).ready(function() { ...

Converting a string array to an array object in JavaScript: A step-by-step guide

My task involves extracting an array from a string and manipulating its objects. var arrayString = "[{'name': 'Ruwaida Abdo'}, {'name': 'Najlaa Saadi'}]"; This scenario arises when working with a JSON file where ce ...

the dropdown menu toggle is not working as expected

My dropdown icon is not appearing and the menu options are not functioning properly. Despite trying to use a script to display the text when an option is clicked, it doesn't seem to be working. It appears that the toggle functionality is not working ...