JavaScript Pattern: Using Custom Events

Поделиться
HTML-код
  • Опубликовано: 26 мар 2019
  • In JavaScript you can create a custom event and respond to that event using a listener. In this tutorial we will show you how to create a custom event, emit that event, and respond to it with an event listener.
    Would you like to help keep this channel going?
    / allthingsjavascript
    For more resources on JavaScript:
    www.allthingsjavascript.com
    Access to EVERY course (get 2 months free): www.skillshare.com/r/profile/...
    Courses offered on Udemy at a discount:
    Getting Started: www.udemy.com/learn-modern-ja...
    Advanced Topics: www.udemy.com/learn-modern-ja...
    Mastering Regular Expressions in JavaScript:
    www.udemy.com/mastering-regul...
    Functional Programming in JavaScript: www.udemy.com/functional-prog...
    NEW Mastering JavaScript Arrays: www.udemy.com/course/masterin...
    Tutorials referred to in this video:
    Understanding Asynchronous JavaScript: • Understanding Asynchro...
    Event Bubbling: • JavaScript Questions: ...
    #javascript #AllThingsJavaScriptLLC

Комментарии • 21

  • @travezripley
    @travezripley 5 лет назад +1

    Thank you, please keep the videos coming!! So good!

  • @adarshpandey8526
    @adarshpandey8526 2 года назад

    Simply awesome short video. It completely made sense to me throughout. I'm gonna implement this for all my projects moving forward.
    Thanks for your efforts ❤️

  • @levk4195
    @levk4195 3 года назад

    Just found the video, awesome job!! Works great

  • @daegudude1048
    @daegudude1048 3 года назад

    This is so gold, thanks a lot :)

  • @DineshGgoogle
    @DineshGgoogle 5 лет назад

    Wow thanks a lot. very helpful

  • @AbhishekKumar-mq1tt
    @AbhishekKumar-mq1tt 5 лет назад +1

    Thank u for this awesome video

  • @Rajeshkumar-jb9pf
    @Rajeshkumar-jb9pf 5 лет назад +2

    Great!

  • @janulik9535
    @janulik9535 2 года назад

    thx for nice explanation

  • @Serjgap
    @Serjgap 5 лет назад

    how about you talk about Shadow DOM
    And using event bubbling to communicate between webcomponents

  • @russelldriver6394
    @russelldriver6394 3 года назад

    Why is it I cannot have a custom event created, subscribed to and listened to in the same js file, it only seems to work if I have 2 seperate script files being loaded in? I didnt see your html setup in the video.

    • @AllThingsJavaScript
      @AllThingsJavaScript  3 года назад

      You should be able to do it in the same file. Are you getting an error?

    • @TheCodeCreative
      @TheCodeCreative 2 года назад

      Make sure you have your addEventListener code come before your dispatchEvent code.

    • @russelldriver6394
      @russelldriver6394 2 года назад +1

      @@TheCodeCreative Thanks.

  • @user-ty5pd4xw2y
    @user-ty5pd4xw2y 2 года назад

    Hi, your code doesn't work in 1 single file... absolutely nothing happens... why? thanks for some clues

    • @Russ_Paul
      @Russ_Paul 2 года назад +2

      The eventListener handler needs to be added/registered before you dispatch the event. e.g.
      const loginEvent = new CustomEvent('login', {
      detail: {
      firstTime: true
      }
      })
      // listener added to document first
      document.addEventListener('login', (event) => {
      console.dir(event)
      })
      // can now dispatch the event and it will be caught
      document.dispatchEvent(loginEvent)

    • @user-ty5pd4xw2y
      @user-ty5pd4xw2y 2 года назад +1

      @@Russ_Paul thanks Paul)

    • @Russ_Paul
      @Russ_Paul 2 года назад

      @@user-ty5pd4xw2y You're welcome.

    • @AllThingsJavaScript
      @AllThingsJavaScript  2 года назад

      Can you supply the code you used?

  • @longjohnny
    @longjohnny Год назад

    Is there ANY way to have a parent element emit/ dispatch an event which can be 'heard' by its child elements?

    • @AllThingsJavaScript
      @AllThingsJavaScript  Год назад

      I believe you can do this with some JavaScript libraries, but this is not something I have tried.