bhylu.com

Advertisements

jQuery DOM

jQuery event delegation attaches one handler to a stable parent. Native addEventListener can express the same pattern in new code.

Where to run this

Use a local browser project for this example. It needs browser permissions, additional files, a server endpoint, or a library as described below. The restricted playground cannot provide those dependencies.

A practical example

JavaScript

// HTML: <div id="tools"><button>Brush</button></div>
// Load jQuery first.
jQuery("#tools").on("click", "button", function () {
  console.log(jQuery(this).text());
});

What happens

The delegated handler reports the clicked tool button.