4 ms·
That page does an excellent job of convincing me to carry on using jQuery. if (el.classList) el.classList.contains(className); else new RegExp('(^|
by andybak 10y ago
That page does an excellent job of convincing me to carry on using jQuery.
if (el.classList)
el.classList.contains(className);
else
new RegExp('(^| )' + className + '( |$)', 'gi').test(el.className);
vs
$(el).hasClass(className);
<shivers>
- oneeyedpigeon 10y agoI don't think anyone would suggest you write all of the first codeblock every time you want to check a class. You'd put it in a function and call that, much like jquery does. The point is that this site explains how you do it because a) that's probably easier than trawling through the jquery source b) including this polyfill when you know you need it might save you from requiring all the rest of jquery.
- andybak 10y agoIn that case I'd be much more likely to use a minimal jQuery replacement such as umbrella.js mentioned elsewhere here. That's 3k (vs about 28k for jQuery) so counters most of the 'bloat' objections with jQuery. I'm sure it will still irk some some purists on other grounds however.
- edgarvaldes 10y agoYep, the use of RegExp is enough.