I add mousedown event listener like this in a js file. "adding event listener" log is visible. But when i click list items "test" log doesn't appear in console. if i select a regular vanilla html element with "querySelectorAll" i can see "test" log if i click that element.
setTimeout method makes the difference. Solved thanks. But I don't understand how that works. I see that even though we don't provide a time value for setTimeout method it will not execute the code immediately:
Due to JavaScript's event loop mechanism, this does not execute immediately but runs after the current code block has completed.
When I debug that JS code (without setTimeout), specifically "item" variable, I can see "li" tag in that. I have the correct element but event doesn't attach:
document.querySelectorAll(selector).forEach(item => {
//I have the "li" element here.
item.addEventListener('mousedown', (event) => console.log('test'));
});
I think the list view is not fully initialized when you run the code without setTimeout. Perhaps it is later updated by Blazor hence the event handlers are wiped.