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

22 Replies, 1970 Views

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!

Messages In This Thread



Users browsing this thread: 1 Guest(s)