How can I get the element that is clicked on with a click handler added in a forEach?

y e n i m a l

I need to detect element count number

I have 10 poster elements and I have a forEach loop

I need the poster number that I clicked on, for example if I click on the seventh poster posternumber should be 7

    poster.forEach(p => {
            p.addEventListener('click',function(){
                videoTag.src = xResult[movieLanguageUrl][/*posternumber*/]; 
           });
    });
Diogo Capela

Get the index value on the second parameter of the forEach function and it should work:

poster.forEach((p, index) => {
    p.addEventListener('click', () => {
        videoTag.src = xResult[movieLanguageUrl][index + 1]; 
   });
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I get css property of an element which clicked in Blazor?

Button Group Click Handler - How to get text content of clicked button?

How to get the ID of a clicked element that was added after the document was created?

How can I get element from list in JSTL (not forEach)?

How can I add javascript to the head of an html document to listen for a click on an element not yet added to the DOM?

How can I get an output by click on a select2 element?

How can i get mouse click event element in cocoa

How can I get selected text on a element click event?

How can I detect specific element is clicked?

How can I remove an element added by jquery?

Python Logging: How can I determine when a handler was added?

Why is my jQuery click handler acting as though I clicked on the parent element?

How can I click a button and have several other buttons get clicked?

How to get the highest parent element of an element that was clicked on that is still a child of the element that the event listener was added to?

how can $(this) not be the element that triggered the event in a jquery click handler?

How can I get the selected option of the closest element of my clicked box?

How can I get the email id of the clicked span element which is hidden property in css

How can i get clicked link href?

How can I get number of added subscriptions?

How can i get columns added on column?

Grab attribute of element clicked - using on click event handler

How i can add click handler function to dynamically link in Vue?

How can I add a Click Event Handler to a DataGrid Context Menu?

How can I update a page from a click handler on another page?

how can I make a button click function be clicked multiple times?

How can i change click function when clicked second time

Get data from dynamically added element on click

How can i add class to a image element in quill image Handler?

How can I display an alert with the position of the element that was clicked?