Comments on: The classList API https://stevefaulkner.github.io/html5doctored/the-classlist-api/ helping you implement HTML5 today Thu, 28 Jul 2016 17:36:34 +0000 hourly 1 By: Jan https://stevefaulkner.github.io/html5doctored/the-classlist-api/#comment-2131947 Sun, 10 Jul 2016 02:54:08 +0000 http://html5doctor.com/?p=5206#comment-2131947 From the following page:

https://github.com/eligrey/classList.js

I saved the classList.js script in the same folder as my web page, and reference it like this:

But IE8 still gives error on the following line:

document.getElementById(“myDropdown1”).classList.toggle(“show”);

Any input on this would be appreciated!

Thanks.

]]>
By: allonestring https://stevefaulkner.github.io/html5doctored/the-classlist-api/#comment-2131370 Mon, 04 Jul 2016 20:20:11 +0000 http://html5doctor.com/?p=5206#comment-2131370 classList.item() was just what I needed – thank you ☺

]]>
By: Richard Hunter https://stevefaulkner.github.io/html5doctored/the-classlist-api/#comment-2054135 Sat, 07 Nov 2015 19:54:12 +0000 http://html5doctor.com/?p=5206#comment-2054135 It would be nice if add() would take an array of classes but it seems to only half work( i.e. not at all!) producing a weird comma separated string.

You can use apply() though to do the same thing:

var el = document.getElementById('myel');

el.classList.add.apply(el.classList, ['red', 'green']);

Js fiddle demonstrating this here:

here

Works in latest desktop Safari, Chrome and Firefox at least.

]]>
By: Uxus https://stevefaulkner.github.io/html5doctored/the-classlist-api/#comment-49872 Wed, 28 May 2014 06:13:16 +0000 http://html5doctor.com/?p=5206#comment-49872 You have duplicate declaration of `classes` variable in your `addmany` and `removemany` methods. The var keyword is not necessary there cause `classes` variable is already declared. And I modified the mthods so they don’t have explicit variable declarations:

DOMTokenList.prototype.addmany = function(classes) {
classes.split(' ').forEach(function(cls){
if(!this.contains(cls)){
this.remove(cls);
}
}, this);
};

This of course not so fast as `for loop` but more readable.

]]>
By: Chris Riesbeck https://stevefaulkner.github.io/html5doctored/the-classlist-api/#comment-49077 Fri, 18 Apr 2014 20:28:06 +0000 http://html5doctor.com/?p=5206#comment-49077 Nice writeup and so happy to find classList exists. But to remove all classes or replace with a new list, why not simply set className? No loops needed at all.

]]>
By: Serkan Sipahi https://stevefaulkner.github.io/html5doctored/the-classlist-api/#comment-48734 Mon, 17 Mar 2014 09:55:33 +0000 http://html5doctor.com/?p=5206#comment-48734 hi,

and if you want to find/know where come “DOMTokenList” then you can do this:

var r = document.createElement(‘div’).classList.constructor.name;

r === “DOMTokenList” // :)

]]>
By: Judaica https://stevefaulkner.github.io/html5doctored/the-classlist-api/#comment-39670 Wed, 13 Nov 2013 21:56:34 +0000 http://html5doctor.com/?p=5206#comment-39670 The classlist. certainly does it for me. A great way to manipulate and allow interaction. I’m all for it.

]]>
By: Binyamin https://stevefaulkner.github.io/html5doctored/the-classlist-api/#comment-37413 Tue, 13 Aug 2013 07:17:20 +0000 http://html5doctor.com/?p=5206#comment-37413 Cannot toggle multiple classes element.classList.toggle('one', 'two').

]]>
By: Binyamin https://stevefaulkner.github.io/html5doctored/the-classlist-api/#comment-37412 Tue, 13 Aug 2013 07:12:46 +0000 http://html5doctor.com/?p=5206#comment-37412 Both
element.classList.remove('one', 'two', 'three');
and
element.classList.add('one', 'two', 'three');
are supported on Chrome 28 stable and Opera 15.
Multiple classes remove/add not supported even on Firefox Nightly.

]]>
By: zzzzBov https://stevefaulkner.github.io/html5doctored/the-classlist-api/#comment-37336 Thu, 08 Aug 2013 14:43:21 +0000 http://html5doctor.com/?p=5206#comment-37336 @Derek Johnson, if the future implementation is not compatible with the implementation provided by the prototype extension, your usage of the prototype extension will break.

Worse, if you then decide to use a library that uses feature detection for features on the classList API, the library will be tricked into believing that the browser supports features that it doesn’t, and would then use the incompatible implementation.

You’re better off making a utility class or set of functions to fill in the gaps, while not interfering with the browser’s native objects.

A good example is how Historyjs creates the History object which calls the native history object if it exists.

]]>