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

From Dev

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

From Dev

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

From Dev

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

From Dev

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

From Dev

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

From Dev

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

From Dev

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

From Dev

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

From Dev

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

From Java

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

From Java

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

From Dev

Call a javascript function from select element in HTML

From Dev

Call javascript function by clicking on HTML list element

From Dev

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

From Dev

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

From Javascript

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

From Dev

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

From Dev

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

From Dev

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

From Dev

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

From Dev

Javascript returns the same id for every iterated element in html

From Dev

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

From Dev

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

From

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

From Dev

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

From Dev

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

From Dev

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

From Dev

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

From Dev

how can i call element by using this in jquery

Related Related

  1. 1

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

  2. 2

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

  3. 3

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

  4. 4

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

  5. 5

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

  6. 6

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

  7. 7

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

  8. 8

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

  9. 9

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

  10. 10

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

  11. 11

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

  12. 12

    Call a javascript function from select element in HTML

  13. 13

    Call javascript function by clicking on HTML list element

  14. 14

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

  15. 15

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

  16. 16

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

  17. 17

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

  18. 18

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

  19. 19

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

  20. 20

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

  21. 21

    Javascript returns the same id for every iterated element in html

  22. 22

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

  23. 23

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

  24. 24

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

  25. 25

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

  26. 26

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

  27. 27

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

  28. 28

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

  29. 29

    how can i call element by using this in jquery

HotTag

Archive