Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

JavaScript cheat sheet

From MoodleDocs
Revision as of 14:56, 29 October 2020 by David Mudrak (talk | contribs) (Starting this page as kind of personal cheat sheet of useful snippets as I am getting familiar with ES6 and un-learning YUI and jQuery style of doing things.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Locating DOM elements

// Get element by identifier. let root = document.getElementById('uniqueidentifier');

// Get element within another element by attribute. let widget = querySelector('[data-widget="widgetname"]');

// Find all elements within another element and do something with them. let missingItems = root.querySelectorAll(':scope [data-region="itemname"].missing'); missingItems.forEach(item => {

   window.console.log(item);

});

// Find the closest parent element matching the selector. let region = widget.closest('[data-region]');