question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Blazor Question] How to still use client side addEventListener

See original GitHub issue

I’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:closed
  • Created 4 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
SteveSandersonMScommented, Jan 2, 2020

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’s OnAfterRenderAsync 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/

0reactions
pranavkmcommented, Dec 30, 2019

@SteveSandersonMS could you have a look?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found