I am trying to figure out how I can get a list of elements from the DOM (top to bottom) containing specific attributes that are prefixed with a given string lets say for the sake of example "monkey" is that string. If any exist.
I had this very loosely put together idea using $.each()
and :contains
but between the two I see no good coming from it. So I figured I'd come here to pick a few brains.
What I want to do is basically scan my dom when I call to a given function I will end up building up. That will seek elements with attributes that are prefixed with in this case "monkey" think of the data-
attribute, and its use. Minus providing a specific selector, and not actually using the data-
attribute.
Imagine this HTML
<div monkey-user="bar" monkey-permission="read">
<span monkey-user="bar" monkey-permission="read">some text</span>
</div>
<div>
<input type="text" monkey-user="bar" monkey-permission="none">
</div>
<input type="text">
<input type="text">
<input type="text">
<input type="text">
and then this javascript/jquery concept unless something cleaner can be used.
var arr = new Array();
$("*").each(function()
{
if($(this).children().length == 0)
{
if($(':contains("monkey-")',this))
{
arr.push($(this));
}
}
});
The result of arr in this case would be the 3 elements above with monkey-
in them otherwise anything else is skipped. also worth noting is user/permission is for the sake of example, in the long run those can be other specific things that will be worked with on a per need basis (those in that case will be similar to working with the data
attributes but Im thinking that will be another question for another time less someone wants to throw that in as a bonus.