I want to use JavaScript to find a specific HTML element by the number of characters in its id
attribute.
For example if the element I want to find looks like this:
<element id="12345678"></element>
And has 8 characters in its id
attribute, how would I find this element.
Some basic pseudo code I imagine would look like this:
[e] = document.GetElementById where id.charLength == 8
And this would return an array with all elements on the page whose id
attribute is 8 characters long.
In case anyone asks, I cannot just use the id
to get the element because the id
changes with every page refresh. Xpath will not work either as the element changes its position in the DOM with every refresh. There are no constant identifying attributes of the element's parents either.
The only constant is the length of the id
attribute of this specific element, and so I have concluded that this is the only way to grab the element.