GithubHelp home page GithubHelp logo

Working with Events Questions about reef HOT 2 CLOSED

mouse0270 avatar mouse0270 commented on May 26, 2024
Working with Events Questions

from reef.

Comments (2)

cferdinandi avatar cferdinandi commented on May 26, 2024

Hi @mouse0270, so... it's all about scope.

The Class needs to be in the global scope to call it form an on* event on the element. As an alternative to on events, you can use event delegation without having to wait for the render to happen.

If you are going to attach events directly to the rendered elements, I would use reef:start as your event hook if they're not going to be removed from the UI later.

from reef.

mouse0270 avatar mouse0270 commented on May 26, 2024

Its a module, it doesn't get defined on the global scope. Which is fine, I was just trying to provide an example of why passing in events used to be a very nice feature.

Also, event delegation is great, and you did a fantastic job writing that article, it might be best to update it. Though it does a great job at explaining how to use event delegation in pure vanilla JS, it leaves out the fact that it won't work on any element that has children in it.

<button class="click-me" id="click-me-1">Click Me</button>
<button class="click-me" id="click-me-2"><span>Click Me</span></button>

<script>
// Listen for click events
document.addEventListener('click', function (event) {
  if (!(event.target.matches('.click-me'))) return;
  
  console.log('An element was brought into focus.');
  console.log(event.target);
});
</script>

In this example, you'll find that the second button never triggers the click event unless you click on the very edge of the button because javascript has registered that the span is the target element.

changing the script to something like

// Listen for click events
document.addEventListener('click', function (event) {
  if (!(event.target.closest('.click-me'))) return;
  const element = event.target.closest('.click-me')
  
  console.log('An element was brought into focus.');
  console.log(element );
});

I know this was off-topic, but it was a really well written article and figured it would be a nice addition to include some of the limitations of event delegations and how to get around them.

from reef.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.