[Blazor Question] How to still use client side addEventListener
See original GitHub issueI’m using a bootstrap template (metronic) which has for instance a menu that is collapsible. For that the template makes some plain javascript calls to bind the events. After adding the template to a Blazor solution, the events where not bound. In the original code they have a javascript like this. The code is included in myscripts.js
which is referenced from the _Hosts.cshtml
element.addEventListener(type, handler);
This event does never fire. If I change the event registration to
$("body").on('click', "#myid", myfuction());
it works. So is there a way to still use addEventListener method with Blazor? Or is there anything else I need to make different?
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
How to add client DOM javascript event handler when ...
If I run my client side script which is attaching the event handlers ... addEventListener("click", function() { console.log("click handler ...
Read more >File uploader component not triggering onUploadProgress ...
I'm using a file uploader component in Blazor WebAssembly and I've ... try to use addEventListener method to listen the progress function.
Read more >How to attach an event to ASPxDateEdit on the client side
Hello, I encounter an issue with ASPxDateEdit, I'm trying to add an event handler on client side by using javascripts addEventListener.
Read more >Goodbye Client Side JavaScript, Hello C# Blazor - YouTube
Blazor uses the latest in web standards, WebAssembly. This means no plugins, transpilation, or JavaScript are needed.
Read more >EventTarget: addEventListener() method - Web APIs | MDN
The addEventListener() method of the EventTarget interface sets up a function that will be called whenever the specified event is delivered ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
document.getElementById
can only return references to elements that exist within the DOM now. If a new matching element gets added later, that’s a new element and your code hasn’t added any event handlers to it yet.The fact that elements can be added and removed dynamically is the whole reason why jQuery has the
.on
function. If you want to add event listeners to all current and future matching elements, then you should use.on
or something like that.This isn’t specific to Blazor but would apply to all technologies that modify the DOM dynamically.
There is a more idiomatic way to approach this in Blazor. Instead of putting your JS code directly inside your
_Host.cshtml
(where it can only run once), consider invoking the initialization logic from a Blazor component’sOnAfterRenderAsync
method, so that component’s rendered elements get initialized whenever it is first rendered. Here’s a tutorial: https://blazor-university.com/javascript-interop/calling-javascript-from-dotnet/passing-html-element-references/@SteveSandersonMS could you have a look?