Functional approach. Returns an array of all matching elements and aligns spaces during validation.
function getElementsByText(str, tag = 'a') { return Array.prototype.slice.call(document.getElementsByTagName(tag)).filter(el => el.textContent.trim() === str.trim()); }
Using
getElementsByText('Text here'); // second parameter is optional tag (default "a")
if you are viewing different tags, i.e. range or button
getElementsByText('Text here', 'span'); getElementsByText('Text here', 'button');
The default value for the tag = 'a' will need Babel for older browsers
Pawel Jul 13 '17 at 19:54 on 2017-07-13 19:54
source share