I\'m going to use various data attributes names start with for example \"data-mo-\"
.
Assume I have these elements:
If all you wish to do is find whether a given node has an attribute beginning with a specific string, then one approach is the following:
// node: a single HTMLELement node,
// attr: the string to test against the attribute names:
function hasAttributeStartingWith(node, attr) {
// here we return the result, using Array.from() to turn
// the node-map of attributes into an Array, and then
// Array.prototype.filter() to remove any attributes that
// do not return a true/truthy result against the supplied
// assessment:
return Array.from(node.attributes).filter(function(attributeNode) {
// attributeNode: a reference to the current attribute-node
// of the array of attribute-nodes over which we're iterating.
// here we test to see if the nodeName (the attribute-name)
// of the attribute-node begins with the supplied string
// (held in the 'attr' variable):
return attributeNode.nodeName.indexOf(attr) === 0;
// if the filtered array is greater than zero then
// there are some attributes beginning with the
// supplied string:
}).length > 0;
}
// here we convert the nodeList returned from document.querySelectorAll()
// into an Array, using Array.from(), and iterate over those elements
// using Array.prototype.forEach():
Array.from(document.querySelectorAll('span')).forEach(function(span) {
// 'span': a reference to the current element of the
// array of elements over which we're iterating.
// using the function within the 'if' assessment, since it
// returns a Boolean true/false:
if (hasAttributeStartingWith(span, 'data-mo')) {
// using the Element.classList API to add
// the 'hasAttributeStartingWith' class to
// the current if the function returns
// true:
span.classList.add('hasAttributeStartingWith');
}
});
function hasAttributeStartingWith(node, attr) {
return Array.from(node.attributes).filter(function(attributeNode) {
return attributeNode.nodeName.indexOf(attr) === 0;
}).length > 0;
}
Array.from(document.querySelectorAll('span')).forEach(function(span) {
if (hasAttributeStartingWith(span, 'data-mo')) {
span.classList.add('hasAttributeStartingWith');
}
});
.hasAttributeStartingWith {
display: inline-block;
font-size: 1.5em;
color: limegreen;
}
Title 1
Title 2
Title 3
Title 4
JS Fiddle demo.
In the above all elements have an attribute starting with data-mo
, to show it working more specifically, try:
Array.from(document.querySelectorAll('span')).forEach(function(span) {
if (hasAttributeStartingWith(span, 'data-mo-b')) {
span.classList.add('hasAttributeStartingWith');
}
});
function hasAttributeStartingWith(node, attr) {
return Array.from(node.attributes).filter(function(attributeNode) {
return attributeNode.nodeName.indexOf(attr) === 0;
}).length > 0;
}
Array.from(document.querySelectorAll('span')).forEach(function(span) {
if (hasAttributeStartingWith(span, 'data-mo-b')) {
span.classList.add('hasAttributeStartingWith');
}
});
.hasAttributeStartingWith {
display: inline-block;
font-size: 1.5em;
color: limegreen;
}
Title 1
Title 2
Title 3
Title 4
JS Fiddle demo.
This should match only the element which has an attribute starting with the string data-mo-b
, styling only the second element.
References: