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.

Why are you converting inline events to event listeners?

See original GitHub issue

TL;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:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:26 (21 by maintainers)

github_iconTop GitHub Comments

2reactions
rbiggscommented, Mar 16, 2017

And another bytes the dust šŸ˜‰

2reactions
jorgebucarancommented, Mar 16, 2017

@rbiggs Thanks a lot! This saved us like 200 bytes, give or take. šŸŽ‰

Read more comments on GitHub >

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

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