JSFiddle - React, Tailwind, and code Playground
HTML
<div id="thing">
</div>
<div id="otherThing">
<div id="sibling">
</div>
<div id="target9999">
</div>
<div id="nextSibling">
<div id="target999">
<div id="target93">
</div>
</div>
</div>
</div>
JavaScript
document.findElementWithIdLike = function(prefix) {
return findChildWithIdLike(document.body, prefix);
};
document.findElementsWithIdLike = function(prefix) {
var results = [];
findChildrenWithIdLike(document.body, prefix, results);
return results;
};
window.findChildWithIdLike = function(node, prefix) {
if (node && node.id && node.id.indexOf(prefix) == 0) {
//match found
return node;
}
//no match, check child nodes
for (var index = 0; index < node.childNodes.length; index++) {
var child = node.childNodes[index];
var childResult = findChildWithIdLike(child, prefix);
if (childResult) {
return childResult;
}
}
};
window.findChildrenWithIdLike = function(node, prefix, results) {
if (node && node.id && node.id.indexOf(prefix) == 0) {
//match found
results.push(node);
}
//check child nodes
for (var index = 0; index < node.childNodes.length; index++) {
var child = node.childNodes[index];
var childResult = findChildrenWithIdLike(child, prefix, results);
if (childResult) {
results.push(childResult);
}
}
};
alert(document.findElementsWithIdLike("target").length);