Skip to content

Commit cac0c1d

Browse files
authored
Refactor JS event handler examples (#33335)
1 parent 45cd6ad commit cac0c1d

File tree

3 files changed

+193
-89
lines changed

3 files changed

+193
-89
lines changed

aspnetcore/blazor/components/js-spa-frameworks.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,17 @@ In the following example:
117117
`wwwroot/scripts.js`:
118118

119119
```javascript
120-
async function showQuote() {
120+
window.showQuote = async () => {
121121
let targetElement = document.getElementById('quoteContainer');
122122
await Blazor.rootComponents.add(targetElement, 'quote',
123123
{
124124
text: "Crow: I have my doubts that this movie is actually 'starring' " +
125125
"anybody. More like, 'camera is generally pointed at.'"
126126
});
127127
}
128+
129+
const btn = document.querySelector("showQuoteBtn");
130+
btn.addEventListener("click", showQuote);
128131
```
129132

130133
After the [Blazor script](xref:blazor/project-structure#location-of-the-blazor-script) is loaded, load the preceding scripts into the JS app:
@@ -140,7 +143,7 @@ In the preceding example, the `{BLAZOR SCRIPT}` placeholder is the Blazor script
140143
In HTML, place the target container element (`quoteContainer`). For the demonstration in this section, a button triggers rendering the `Quote` component by calling the `showQuote` JS function:
141144

142145
```html
143-
<button onclick="showQuote()">Show Quote</button>
146+
<button id="showQuoteBtn">Show Quote</button>
144147

145148
<div id="quoteContainer"></div>
146149
```

0 commit comments

Comments
 (0)