cp-classlist

1.0.1 • Public • Published

Cross Platform ClassList (cp-classlist)

Cross Platform ClassList is a cross platform library (IE8+) used for manipulating the classlist of a DOM element.

Installation

Use the package manager [npm] to install cp-classlist.

npm install cp-classlist

Usage

 
var cpClassList = require('cp-classlist');
 
var element = document.getElementById('id'); // Or you can use querySelector
 
var list = cpClassList(element);
 
//Methods
 
//GET CLASS LISTS
 
list.getList(); // returns an array of class names of the element.
 
//ADD CLASS LISTS
list.add(class1, class2, class3, ...); //Adds classes passed and returns the object.
 
//REMOVE CLASS LISTS
list.remove(class1, class2, class3, ...); //Removes classes passed and returns the object.
 
//TOGGLE CLASS LISTS
list.toggle(className); // Toggles between the class name passed and returns the object.
 
//ITEM IN CLASS LIST
list.item(number); // Returns the className of the index passed else false; Note: It throws error when number is not passed
 
//CONTAINS
list.contains(className); // Returns true if the className is found else false
 
 
//REPLACE CLASSNAME
list.replace(oldClassName, newClassName); // If old className is found it replaces it with the new className and returns the object else it returns false
 
 
 
//Methods that can be chained
 
add(), remove(), toggle()
 
//EXAMPLE
 
list.add('foo', 'another-class').remove('prev-class').toggle('a-class-name')
 
//NOTE THAT WHEN replace() 's oldClassName is found and gets Replaced it returns an object which can be chained with other methods
 
 
 

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

MIT

Package Sidebar

Install

npm i cp-classlist

Weekly Downloads

2

Version

1.0.1

License

MIT

Unpacked Size

19.9 kB

Total Files

5

Last publish

Collaborators

  • mkhstar