Juan Manuel Llaury
Juan Manuel Llaury

Reputation: 63

Is it possible to getElementsByClassName with partial name on Vanilla 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

Upvotes: 6

Views: 4164

Answers (2)

Valometrics.com
Valometrics.com

Reputation: 148

you can use:

document.querySelectorAll("[class$=sarasa]")

Upvotes: 0

Robin Zigmond
Robin Zigmond

Reputation: 18249

No, 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

Upvotes: 17

Related Questions