Adding functionality to elements by looping through them in Javascript

Seeking assistance in adding an Event Handler for each of my <td> elements. I attempted the code below:

var allSquares = document.getElementsByTagName("td");

for (var i = 0, len = allSquares.length; i < len; i++){
allSquares[i].addEventListener('ondragover', allowDrop, false);

}

Curious as to why this approach did not yield the desired result. Any insights are greatly appreciated.

Answer №1

Typically, the event names used with addEventListener do not include the prefix "on".

allSquares[i].addEventListener('dragover', allowDrop, false);

Answer №2

When using the W3-method addEventListener, there is no need to include an "on" before the event's name (unlike for Microsoft):

allSquares[i].addEventListener('dragover', allowDrop, false);

For more information on advanced event registration models, you can visit Quirksmode's article.

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

Only retrieve links that don't have the specified class by using regular expressions

I am trying to extract all links from an HTML document using REGEX, except for those with a specific class name. For example: <a href="someSite" class="className">qwe</a> <a href="someSite">qwe</a> My goal is to only capture the ...

Navigate to the end of a container

Is there a method to automatically scroll to the bottom of a div when the page is loaded? I have attempted several solutions without success. If you have any insights, please share them. Thank you! ...

The AngularJS framework is failing to disable the autocomplete feature for the input field with a password type

I have attempted to disable auto-complete for the password input, but it doesn't seem to be working. Below is a sample of my code: <form name="testfrm" ng-submit="test(testfrm)" autocomplete="off"> <input type="password" id="passwor ...

Maintaining state value during client-side navigation in NextJs with Next-Redux-Wrapper

Currently, I am working on resolving the hydration issue that occurs when using wrapper.getServerSideProps. The problem arises when I reroute with the existing setup and the store gets cleared out before adding new data. This leads to a blank page as essen ...

Setting cursor position in input field when navigating with arrow keys: What are the ways to achieve accessibility?

I have implemented arrow key navigation in a table, allowing navigation with the up, down, left, and right arrow keys. How can I ensure that the cursor always stays on the right side of the input field during navigation? Check out my Stackblitz example he ...

The history.push function seems to be leading me astray, not bringing me back

Issue with History.Push in Register Component App function App() { const logoutHandler = () =>{ localStorage.removeItem("authToken"); history.push("/") } const [loading, setLoading]= React.useState(true) useEffect(()=>{ ...

Ways to trigger a server function in JavaScript upon closing a browser tab or the entire browser

How can I trigger a server function when closing a browser tab or window using JavaScript? Similar to this image //Below is the code for my server-side function: public ActionResult DeleteNotPostedImage(string folder , string PostID) { folder ...

Ways to showcase an item within additional items?

I'm struggling to properly display data in a table. My goal is to iterate through an object within another object inside an array and showcase the source, accountId, name, and sourceId in the table. https://i.sstatic.net/VVIuc.png <tbody clas ...

Can you fill two dropdown menus with an array of choices?

I'm encountering an issue with using an array to populate two different select boxes. The first select box gets populated correctly, but when trying to add the same list to the second select box, it clears the first one and automatically selects the l ...

React: Update the hidden input's value to reflect the total number of spans clicked

I have a component called Rating that displays a star rating like this: var Rating = React.createClass({ render: function () { return ( <div className="rate-line"> <div className="rate-title">{this.props ...

Implementing Object-Oriented Programming (OOP) to insert data into a database in PHP

Seeking a method to insert data into a MySQL database without the need to refresh the page and ensuring no security vulnerabilities. After several hours of experimenting and researching, I have come up with this solution: <form name="form1" action="" ...

Preserve the variable alterations for future promises

I have implemented promises in my database access using elasticsearchjs, which utilizes Bluebird. Each ID in the list triggers a new query, and I need to identify the failing query by its ID. const idList = ['id1', 'id2', 'id3&apo ...

React: Welcome page/enclosure

https://i.sstatic.net/kkiVC.png My current setup includes a page with a container that houses a small game along with a score/time bar for tracking progress within the game. Now, I want to introduce a splash screen that appears at the start of the game a ...

Update all other input fields whenever a single field is modified. Implement using AngularJS (Link to Plunker included)

Check out this plunker example at http://plnkr.co/edit/Ll09uMtJEC0HqyGBRPj?p=preview Within the plunker, there are input fields for date, user, and car. It is possible to select values for all three fields. If I attempt to change the date, I would like ...

"Enhance your website with Zend 1.12's Ajax capabilities and revolutionize

When the user submits a form, I want to trigger Zend validation for that form without refreshing the entire page. My website also utilizes zend_Layout, yet despite consulting numerous tutorials, I have been unable to successfully implement this functionali ...

The Material UI Icon rendered using document.createElementNS in React is failing to load properly

I'm currently developing a tags section for my app, similar to the tags feature in a new Stack Overflow question form. For this, I am utilizing the Material UI close icon. You can see how it is implemented in this tutorial on YouTube. (I have confirme ...

Is there a method to incorporate the ".then callback" into useEffect?

Is there a way to utilize the ".then callback" to log the word "frog"? I am interested in viewing my token within the async-storage. Once I have obtained my token, I need to append it to the header of a fetch call. Does anyone know how to accomplish this? ...

Angular data binding between an input element and a span element

What is the best way to connect input texts with the innerHTML of a span in Angular6? Typescript file ... finance_fullname: string; ... Template file <input type="text" id="finance_fullname" [(ngModel)]="finance_fullname"> <span class="fullnam ...

Is there a way to retrieve the name of the script called from the package.json file in NodeJS code?

Is there a way to determine which script from package.json has been executed in NodeJS code? A specific script is defined in my package.json file for building the application: { "name": "notes-app", "version": "0.0.1", "license": "MIT", " ...

Controller unable to properly increment values

$scope.isChecked = function(id){ var i=0,j=0,k=0; //$scope.abc[i].usertype[j].keywords[0].key_bool=true; if($scope.abc[i].type_selected == true){ while($scope.abc[i].usertype.length){ while($scope.abc[i].userty ...