[b]"How do I target an HTML query selector element that contains a partial class?"[/b] or [b]"What's the best way

22 Replies, 1751 Views

Title: *"How do I target an html query selector element that contains a partial class?"*

Hey folks!

Struggling a bit here—trying to select an html query selector element that contains a partial class. Like, say I have a class like `btn-primary-large`, but I only wanna target elements with `btn-` in the class name.

I know `querySelector` can do exact matches, but what's the trick for partial matches? Tried `[class*="btn-"]`, but not sure if that's the *best* way or if there's something cleaner.

Any tips? Maybe a regex solution or am I overcomplicating it?

Thanks in advance! 🙌

(Also, if this has been asked before, sorry—my search-fu is weak today.)
Hey! The `[class*="btn-"]` approach you mentioned is actually one of the cleanest ways to target an html query selector element that contains a partial class.

If you wanna get fancy, you could use `document.querySelectorAll('[class^="btn-"]')` to target classes that *start* with "btn-".

Regex might be overkill unless you're dealing with super complex patterns. Stick with CSS selectors—they’re fast and readable.

Check out MDN’s guide on attribute selectors for more tricks!
Yo! `[class*="btn-"]` is totally valid, but watch out—it’ll also match stuff like `disabled-btn-primary` if that’s not what you want.

If you need *only* classes that *begin* with "btn-", use `[class^="btn-"]` instead.

For regex fans, you *could* do `document.querySelectorAll('[class]').filter(el => el.className.match(/btn-/))`, but that’s slower. CSS selectors FTW!
The `*=` operator is perfect for partial matches in an html query selector element. No need for regex unless you’re doing something wild.

Pro tip: If your classes have multiple names (like `btn-primary large`), `[class~="btn-"]` won’t work—it’s for whole words. Stick with `*=` or `^=` for flexibility.

MDN’s docs on attribute selectors are gold for this.
`[class*="btn-"]` is the way to go! It’s simple and works across all browsers.

If you’re using jQuery (I know, old school), you could do `$('[class*="btn-"]')`, but vanilla JS is just as easy.

Regex is overkill unless you’re parsing dynamic classes with weird patterns. Keep it simple!
Hey! For targeting an html query selector element with a partial class, `[class*="btn-"]` is solid.

But if you’re worried about performance, `document.querySelectorAll('[class^="btn-"]')` might be faster since it checks the start of the string.

Regex would work (`/btn-/`), but it’s heavier. Only go there if CSS selectors can’t handle your case.
`[class*="btn-"]` is your friend here! It’s the most straightforward way to match partial classes in an html query selector element.

If you’re dealing with multiple classes, just remember `*=` looks for the substring *anywhere* in the `class` attribute.

For more complex stuff, check out CSS-Tricks’ guide on attribute selectors—super helpful!
The `*=` selector is perfect for this! It’s how I always target partial classes in an html query selector element.

If you’re using frameworks like React or Vue, though, double-check how they handle dynamic classes—sometimes the DOM isn’t what you expect.

Regex is fun but usually unnecessary for this. Stick with CSS!
Thanks everyone! `[class*="btn-"]` worked like a charm—didn’t realize it was that straightforward.

I tried the regex approach too, but yeah, it felt clunky compared to CSS selectors.

One follow-up: What if I need to exclude elements with *both* `btn-` and `disabled`? Would `[class*="btn-"]:not([class*="disabled"])` work?

Y’all are lifesavers! 🙏
`[class*="btn-"]` is the move! It’s clean and does exactly what you need for partial class matching.

If you’re paranoid about edge cases (like `old-btn-new`), `[class^="btn-"]` or `[class|="btn"]` might help.

Regex is cool but way heavier. Only use it if CSS can’t hack it.



Users browsing this thread: 1 Guest(s)