Urgent.News

What's breaking now, across thousands of outlets.

Tech

Some js here for a change (getElemsByDataset)!

Explanation: As I'm working on the development of a contentEditable based editor, I make use of the dataset attribute intensively. Mostly I work with the DOM tree directly but there are situations that I have to collect the elements . For that I use document.getElementsByTagname (this wrapped in a method,called ' getTagNames() ') . It's okay and I get things done with it. Just sometimes it would…

In a recent development for a contentEditable based editor, the author sought a more efficient way to collect elements with different tag names based on a shared dataset name/value pair. The built-in document.getElementsByTagname and document.getElementsByClassName methods had their limitations, so the author proposed a custom function to achieve this goal.

The proposed function, named getElementsByDatasetNameValue, would allow users to assign a related dataset name/value to a group of elements and retrieve them with a single call, regardless of their tag names. This approach would simplify the process of collecting elements with similar properties and improve overall efficiency in the development of the contentEditable editor.

The author began by examining existing HTML methods and their construction to gain a better understanding of how they worked. After researching whatwg.org, the author decided to create a custom class and export function to implement the desired functionality. The function, named getElemsByDataset, would take four arguments: parent_el (the nearest element that contains the desired dataset elements), data_name (the dataset name), data_val (an optional dataset value), and log (a boolean value indicating whether to display the results in the developer console).

The author outlined the steps to create the function, which involved first creating an async method to collect the elements based on the parent element and log option. Next, they created a for of loop to iterate through the unique array of tags, collecting elements with the specified dataset name and value in the constructor. The function would then return the collected elements as an array.

To use the new function, users would simply need to copy and paste the provided code into their script folder and import it into their project. The function accepts four arguments, making it easy to integrate into existing codebases. The author provided an example use case, demonstrating how the function can be used in the context of a contentEditable editor.

In conclusion, the development of the getElementsByDatasetNameValue function provides a more streamlined and efficient way to collect elements based on dataset name/value pairs in a contentEditable based editor. By offering a custom solution to a common problem, the author contributes to the overall improvement of web development practices and empowers developers to create more powerful and versatile applications.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

More from Sunday 27 September →