I want to get an array of all attributes of an HTML element (including their names and values) whose name matches a string.
<div id="myDiv" class="myClass" myPrefix-template="artist-group" myPrefix-records="sculptors" myPrefix-caption="People Who Sculpt"></div>
How to get an array of all the attribute-objects who's name starts with myPrefix-
?
This doesn't work:
let myDiv = document.querySelector("#myDiv");
let attribs = myDiv.attributes;
let dataAttribs = attribs.filter(attrib => {
return attrib.name.includes('myPrefix-');
});
It seems this code should work. It's based on this: https://masteringjs.io/tutorials/fundamentals/filter-array-of-objects
The following works:
const characters = [
{ name: 'MT-caption', value: 'Some People' },
{ name: 'MT-records', value: 'sculptures' },
{ name: 'class', value: 'Deep Space Nine' }
];
tngCharacters = characters.filter(character => {
return character.name.includes('MT-');
});