Event listener is failing to execute the functions

Even though the inline onmouseover="verdadero()" function is properly set up

Upon further inspection, it appears that while the event listener is added to the box element, the function is not being triggered when hovering over it, and console.log is not displayed either

Disclaimer: The HTML code has been simplified for convenience

let onCuadro = false;

const cuadro = document.getElementsByClassName("overscroll__box");

for (let i = 0; i < cuadro.length; i++) {
  cuadro[i].addEventListener("onmouseover", verdadero,true);
}

for (let i = 0; i < cuadro.length; i++) {
  cuadro[i].addEventListener("onmouseleave", falso,true);
}

function verdadero() {
  console.log("onCuadro = true");
  onCuadro = true;
}

function falso() {
  console.log("onCuadro = false");
  onCuadro = false;
}

html :

 <div class="overscroll__box"></div>
 <div class="overscroll__box"></div>
 <script src="./index.js"></script>

Answer №1

When naming events, it is advised not to use the prefix on. Instead, reserve the use of on for situations like:

  • An inline handler, such as <button onclick=,
  • When invoking the IDL getter or setter, like `button.onclick = function() {

However, when utilizing addEventListener, simply specify the event name without the on. For example, you would use mouseover or mouseleave.

for (let i = 0; i < cuadro.length; i++) {
  cuadro[i].addEventListener("mouseover", verdadero,true);
  cuadro[i].addEventListener("mouseleave", falso,true);
}

Alternatively,

for (const box of document.getElementsByClassName("overscroll__box")) {
  box.addEventListener("mouseover", verdadero, true);
  box.addEventListener("mouseleave", falso, true);
}

(It may also be unnecessary to include a third parameter in the addEventListener unless event delegation serves a specific purpose for you)

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

While in Safari, there seems to be a random issue where the embedded image in an SVG becomes blank when using the Canvas API to convert it to

My current approach involves generating a png from a valid svg string that contains an embedded image. The code snippet below illustrates this: <image xlink:href="data:image/png;base64,..." x="0" y="0" width="362" ...

Upload files via Ajax request is required

I am in the process of trying to upload a binary file to a server while avoiding a full page refresh when the server responds. I must admit, I am not well-versed in this area and I understand if my approach needs some adjustments. This is how I have appro ...

User information in the realm of website creation

Can someone provide insight on where user data such as photos, videos, and posts is stored? I doubt it is saved in an SQL database or any similar system. ...

Personalize Autocomplete CSS based on the TextField input in React Material UI when a value is present

In my current project, I am utilizing React Material Autocomplete fields, which includes a nested TextField. I have successfully implemented standard styles for when the field is empty and only the label is visible, as well as different styles for hover ef ...

What steps should I follow to change the appearance of this object to match this?

Attempting to modify the value of an object nested within an array, which is in another object. The nesting might be a bit complex... Here's how it currently looks { household and furniture: [{…}, {…}], school stuffs: [{…}, {…}] } M ...

Perform an HTTP POST request in Angular to retrieve the response data as a JSON object

I am currently in the process of developing a simple user authentication app. After completing the backend setup with Node.js and Passport, I implemented a feature to return JSON responses based on successful or failed authentication attempts. router.pos ...

What are the best circumstances for applying JavaScript in ASP.NET?

As someone who is just starting out in ASP.Net, I have been exploring Validation Controls. However, I am curious about the specific scenarios in which JavaScript would be more appropriate to use. Can anyone provide insight on this? ...

Tips for extracting links from a webpage using CSS selectors and Selenium

Is there a way to extract the HTML links per block on a page that renders in JavaScript? Would using CSS or Selenium be more effective than BeautifulSoup? If so, how would I go about utilizing either of those methods to achieve the extraction of the HTML ...

Generate with different HTML elements

This is a simple React code snippet: var Greetings = React.createClass({ render: function() { return <div>Greetings {this.props.name}</div>; } }); ReactDOM.render( <Greetings name="World" />, document.getElementB ...

Increasing the Efficiency of Styled Components

It appears to me that there is room for improvement when it comes to checking for props in Styled Components. Consider the following code: ${props => props.white && `color: ${colors.white}`} ${props => props.light && `color: ${c ...

Use JavaScript to load and set a background image for a div

When it comes to loading different images onto an "img" tag by printing their URLs using JavaScript and then letting CSS manipulate the content in the tag, I have a code snippet that does just that. $(window).load(function() { var randomImages = [&apo ...

How to effectively utilize JSON responses with jQuery Mobile?

I've been facing an issue with working on my JSON result in JavaScript. Can anyone provide some insight? Even though the JSON call returns a success status code (200) and I can view the data in Firebug, no alert is being displayed. $(document).on(& ...

Tips on displaying the entire text when hovering over it

I'm facing an issue with a select element that retrieves options from an API. The problem is that some options are too large for the text box and get cut off, making them hard to read for users. <div class="form-group my-4"> <lab ...

Ever since updating my jQuery version to 3.2.1, my ajax code seems to have stopped functioning properly

My program's ajax functionality was running smoothly with jquery version 1.7, but when I updated to version 3.3.1, the ajax part stopped working. I made sure to attach the ajax portion of my code after updating the jQuery version. In the PHP file, I s ...

Error occurs in ReactJS App when the state is updated in Redux only after dispatching the same action twice

Check out the following code snippet: ChartActions.js import * as types from './ChartTypes.js'; export function chartData(check){ return { type: types.CHART_DATA,check }; } ChartTypes.js export const CHART_DATA = 'CHART_DATA ...

How to Retrieve Element Property Values from the DOM with JavaScript

I am currently attempting to access the property of an element within my webpage. My main objective is to toggle a float property between left and right when a specific onClick event occurs. However, despite my efforts, I am facing challenges in even acces ...

What is the proper way to correctly invoke NuxtServerInit?

Code snippet from the VUEX repository: export const state = () => ({ z: 'sdfjkhskldjfhjskjdhfksjdhf', }); export const mutations = { init_data_for_firmenistorie2 (state, uploadDbFirmenistorieData){ state.z = uploadDbFirmenistorieD ...

Load data from a MySQL Database into a DataTable

I am currently in the process of developing a CRUD application. With a large dataset stored in a MySQL database, my intention is to utilize a JQuery DataTable instead of manually creating a table. However, the issue I am facing is that while the table appe ...

The parent-child relationships in MongoDB

In my Meteor application, I have created two collections: ActivityCards and Users. Inside the ActivityCard collection, there is a reference to the User like this: { "_id" : "T9QwsHep3dMSRWNTK", "cardName" : "Guntnauna", "activitycardType" : 1 ...

Building a single class in Node.js Express for use across multiple routes

In my project, I am developing APIs in the routes folder. How can I create a validation class to ensure that certain objects are not empty, null, undefined, or contain errors, and then use it across all routes? For instance, when creating a new user via a ...