document.getElementById('foo').classList.remove("bold");
Sure, it's still more verbose. But a lot of what jQuery does is slowly being obsoleted by better native javascript apis.[1] https://developer.mozilla.org/en-US/docs/Web/API/Element.cla... [2] http://caniuse.com/#feat=classlist
If the point of that website was to educate people on the apis that jQuery calls under the hood, it chose a really poor name.
You'd better hope that there actually is an element with an ID of "foo", though, otherwise your example will throw an exception. The last time I pointed this out here, there was some disagreement as to whether jQuery's behaviour is actually desirable. I think it is, but I'm probably biased from writing too much jQuery over the years.
[].map.call(document.querySelectorAll(".foo"), function(node) { node.classlist.remove("bold"); }) [...document.querySelectorAll('.foo')].forEach(node => node.classList.remove('bold')); function ID(id) { return document.getElementById(id); }
saved some typing. I'll always try to use jQuery now though, bigger problems to solve.. few extra bytes to download is not that bad... document.getElementById('foo').className =
document.getElementById('foo').className.replace(/^bold$/, '');
fail if the element in question has more than one class? In that case, you'd need to get the className attribute, split it by spaces, do the replacement in the resulting array, and then join back and assign, I believe.[1]: https://developer.mozilla.org/en-US/docs/Web/API/Element.cla...
document.getElementById('foo').className = document.getElementById('foo').className.replace(/(\s|^)foo(\s|$)/, ' ');
\b is out because it matches - and would kill stuff like foo-bar.So this:
document.getElementById('foo').className =
document.getElementById('foo').className.replace(/^bold$/, '');
Can become to this: foo.classList.remove("x");
And is miles away more performant than slow jQuery.Although, I do occasionally use jQuery, I'm more comfortable and productive when not using it. Also, if I shamelessly take a peek at jQuery's source now-and-then to see how they implement certain features so I can make a helper for it.
You could, but that would require user-agent sniffing (and thus continuous maintenance), and would probably need a pretty unusual caching setup.
document.getElementById('foo').classList.remove('bold');
see https://developer.mozilla.org/en-US/docs/Web/API/Element.cla...The real value of this site is not what it says, but rather a reminder that jQuery is an abstraction and is made of code rather than magick.
Of course, if you were doing that remove class operation quite often, rather than writing all that code out each time, you might wrap it up in a helper method, and maybe you'd call it, I don't know, "removeClass" or something; perhaps if you were doing a whole bunch of DOM operations you might create little helper methods for each one, again to save you some typing. Pretty quickly you might end up with a little library of helper methods... and, of course, being a wonderful programmer who never makes mistakes, I'm sure that little library would be every bit as optimised, reliable, and correct as a battle-tested library used on hundreds of thousands of web sites.