Is there a way I can call a function for each html element when iterated using javascript loops

Robman

I have HTML elements iterated using a javascript loop

The html looks like this

<div class="description">Item 1</div>
<div class="description">Item 2</div>
<div class="description">Item 2</div>

The Javascript

let allItems =  document.querySelectorAll(".description")
  for (let i = 0; i <= allItems.length; i++) {
      allItems[i].addEventListener("click", toggleSpinner.bind(this));
     }
toggleSpinner = () => {
        alert("I clicked") // here should be, for example, "I clicked Item 1,
                           // Or Item 2 or Item 3 depending on which was clicked
      },

How do I call the function independently when each of the element is clicked

hello world

You could use something like this maybe?

toggleSpinner = (data) => {
  console.log(`You clicked item ${data}`) // here should be, for example, "I clicked Item 1,
  // Or Item 2 or Item 3 depending on which was clicked
}


let allItems =  document.querySelectorAll(".description")
for (let i = 0; i < allItems.length; i++) {
	allItems[i].addEventListener("click", toggleSpinner.bind(this, allItems[i].dataset.item));
}
<div class="description" data-item="1">Item 1</div>
<div class="description" data-item="2">Item 2</div>
<div class="description" data-item="3">Item 3</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Is there a more generic way to call JS functions on an HTML element other than putting function calls against each element?

How can I divide each row in a numpy matrix by the last element of that same row, without using loops?

Is there a way to get the values of outer loops when using nested loops in html?

How can I execute parellely a function on each element of a list and collect the status of the function call?

how can i call carousel() an external javascript function into html file?

How can I call a function from javascript module in html onclick

When can I call a function using mutable variables in parallel?

How can I test if a string for a function call using regex in JavaScript?

How i can call function js in enum using javascript/jquery

When using a random parameter in a loop's qualifying comparison, does it call the randomization function once or each time the loops runs?

How Can I call a diferent function for each element stream in Java 8?

Call a javascript function from select element in HTML

Call javascript function by clicking on HTML list element

How can I remove an HTML element using javascript in this specific situation

How can I schedule enable/disable an element in html using javascript?

How can I set focus on an element in an HTML form using JavaScript?

How i can insert an element in between in html using javascript?

How can I remove/delete a HTML element using JavaScript?

Nothing changed when I try to insert an html element using JavaScript

how can I make the array change each time it is iterated?

Javascript returns the same id for every iterated element in html

How do I call more than one function for each case when using a "when" statement in Kotlin?

How can you target a child element of an iterated object in JavaScript?

Can I use two for loops on one function or is there a better way?

how can i store the values i give in html label in to a javascript array and set values for each array element?

Is there any way I should change my call to a Javascript function when I use bind?

I want to map a function to each element of a vector in Theano, can I do it without using scan?

How can I create a HTML/JavaScript element that when I click it it shows a different element in React.js?

how can i call element by using this in jquery