I need to get all innerText of all elements that partially match a given string.
Heres a snippet of the code:
<span class="18794221455sarasa">Some text</span>
The class always ends with 'sarasa' the numbers are dynamic. I want to run it on a chrome extension that's why I need it to be on plain Javascript
I need to get all innerText of all elements that partially match a given string.
Heres a snippet of the code:
<span class="18794221455sarasa">Some text</span>
The class always ends with 'sarasa' the numbers are dynamic. I want to run it on a chrome extension that's why I need it to be on plain Javascript
Share Improve this question asked Feb 14, 2020 at 20:52 Juan Manuel LlauryJuan Manuel Llaury 631 silver badge4 bronze badges 7- Does this help? stackoverflow./questions/3338680/… – user47589 Commented Feb 14, 2020 at 20:54
-
Get all elements that have a
class
, then check whether the class matches the pattern in your loop body. – Barmar Commented Feb 14, 2020 at 20:54 - @Amy The question is about a suffix, not prefix. – Barmar Commented Feb 14, 2020 at 20:55
- @Barmar The top answer also has "contains the substring". Read past the title. – user47589 Commented Feb 14, 2020 at 20:55
- 1 ^ for starting with $ for ending with – EugenSunic Commented Feb 14, 2020 at 20:58
2 Answers
Reset to default 17No, you can't use getElementsByClassName
like that.
But you can use querySelectorAll
with a CSS attribute selector:
document.querySelectorAll('[class$="sarasa"]')
will give you a collection of the DOM elements you want.
See an explanation of this selector here
you can use:
document.querySelectorAll("[class$=sarasa]")