Why are you converting inline events to event listeners?
See original GitHub issueTL;DR Treat inline events like attributes when diff/patching the DOM.
Iāve noticed youāre having lots of issues with events, so I looked at the code. I noticed in the setElementData
method you have the following:
function setElementData(element, name, value, oldValue) {
...
} else if (name[0] === "o" && name[1] === "n") {
var event = name.substr(2)
element.removeEventListener(event, oldValue)
element.addEventListener(event, value)
}
Itās not clear to me why you would need to convert the inline events into event listeners. They are two different things. Inline events are treated like any other attributes on a element. You can set and remove them like so:
// Attach inline event:
document.querySelector(ā#btnā).setAttribute(āonclickā, āalert(āHi!ā)ā)
// Clicking button will alert āHi!ā.
// Now remove inline event:
document.querySelector(ā#btnā).removeAttribute(āonclickā)
// Now clicking button does nothing.
In fact, you can get the inline event on an element through its attributes property:
<button onclick=āalert(āHelloā)ā>Click</button>
document.querySelector(ābuttonā).attributes[0].firstChild === āonclickā
// true
Iām pretty sure what youāre doing there may create duplicate events on documents, leading to various hard to sort out issues.
Or am I missing something here? (possibly)
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:26 (21 by maintainers)
Top Results From Across the Web
Inline Event Handlers vs addEventListener() - Dillion's Blog
The solution to reducing the events in your tag or to completely remove events in your tag (for easy readability - which I...
Read more >Understanding Events in JavaScript | DigitalOcean
Inline event handlers are a straightforward way to begin understanding events, but they generally should not be used beyond testing andĀ ...
Read more >onclick="" vs event handler - javascript - Stack Overflow
Inline event handlers that simply provide pointers to functions is simply wiring up the events and not leaking logic into presentation.
Read more >JavaScript HTML DOM EventListener - W3Schools
The addEventListener() method allows you to add event listeners on any HTML DOM object such as HTML elements, the HTML document, the window...
Read more >Converting Inline-javascript to Javascript - #499 by Paul_Wilkins
Now, we can finish things off by using only one line inside of that forEach loop, to assign the playButton3 function as the...
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 Free
Top 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
And another bytes the dust š
@rbiggs Thanks a lot! This saved us like 200 bytes, give or take. š