JavaScript classListJavaScript classList is a DOM property of JavaScript that allows for styling the CSS (Cascading Style Sheet) classes of an element. JavaScript classList is a read-only property that returns the names of the CSS classes. It is a property of JavaScript with respect to the other properties of JavaScript that includes style and className. The style property returns the color or other styling of the CSS class element, and className is used for returning the names of the classes used in the CSS file. However, the className and classList properties return the name of the classes that we have used in the CSS file but in different ways. The className property returns the name of the classes in the form of a string, whereas the classList property of JavaScript returns the name of the classes in the form of an array. Here, we will take a brief discussion on JavaScript classList and also discuss its methods with their practical implementations. Example of JavaScript classList propertyBelow is an example of JavaScript classList property through which we will get the class value of an element. The output of the above code is shown below: ![]() JavaScript classList PropertyThe classList property is used for representing the value of the class elements which is a DOMTokenList object. It is a read-only property but we can modify its value by manipulating the classes used in the program. The JavaScript classList property consists of following methods through which we can perform different operations on the class elements:
These are some of the methods which are used in the JavaScript classList. We will discuss one by one. classList.add()The function which is used for adding one or more classes to the CSS element. Example: Below example shows how to add a class using classList.add() method: In the code, when the user clicks on the button, the new class get added with the existing classes. The output after clicking on the button is shown below: ![]() classList.remove()The remove() function is used for removing the existing classes from the elements. Below example shows how to remove one or more class using classlist.remove() method: In the above code, when the user clicks on the button, the particular specified class gets removed from the existing CSS classes. The output after clicking on the button is shown below: ![]() Classlist.toggle()The toggle() button is used for toggling classes to the element. It means adding a new class or removing the existing classes. Below is an example that will make us understand how to make use of the toggle() method to add or remove classes. Example: In the code, when the user clicks on the button, the class will get added or removed from the CSS classes. The output after clicking on the button is shown below: ![]() Classlist.contains()The contains() method is used to check whether the specified class exist in the CSS classes and with respect to it, it returns the Boolean value as true or false. Below is an example that shows how to search for a class if it exist or not using the contains() method: Example: In the above code, it is seen that when the user clicks on the button, then it checks for the specified class if present in the CSS classes. If it is present, a Boolean value as true will be returned. Else, it will return false. The output of the above code after clicking on the button is shown below: ![]() classlist.replace()The replace() method is used for replacing an existing class with a new one. It does not mean that the class is removed from the elements but the properties of the existing class get replaced with the properties of the new class. Below is an example through which we will understand that how we can replace an existing class with a new class: Example: In the above code, when the user clicks on the button, the existing class properties get replaced with the new class properties. The output after clicking on the button is shown below: ![]() classList.item()The item() function is used to return the name of the class which is present at the specified index value. Below is an example that will make us understand how to use item() method to return the value: Example: In the code, when the user clicks on the button, the class present on the specified index will get displayed. After clicking on the button, we get the specified index class value, as shown below: ![]() These are some of the methods of the classList DOM object and all about classList. Next TopicJavaScript Code Editors
|
Python tutorial provides basic and advanced concepts of Python.
Vue.js is an open-source progressive JavaScript framework
HTML refers to Hypertext Markup Language. HTML is the gateway ...
Java is an object-oriented, class-based computer-programming language.
PHP is an open-source,interpreted scripting language.
Spring is a lightweight framework.Spring framework makes ...
JavaScript is an scripting language which is lightweight and cross-platform.
CSS refers to Cascading Style Sheets...
jQuery is a small and lightweight JavaScript library. jQuery ...
SQL is used to perform operations on the records stored in the database.
C programming is considered as the base for other programming languages.
JavaScript is an scripting language which is lightweight and cross-platform.
Vue.js is an open-source progressive JavaScript framework
ReactJS is a declarative, efficient, and flexible JavaScript library.
jQuery is a small and lightweight JavaScript library. jQuery ...
Node.js is a cross-platform environment and library for running JavaScript app...
TypeScript is a strongly typed superset of JavaScript which compiles to plain JavaScript.
Angular JS is an open source JavaScript framework by Google to build web app...
JSON is lightweight data-interchange format.
AJAX is an acronym for Asynchronous JavaScript and XML.
ES6 or ECMAScript 6 is a scripting language specification ...
Angular 7 is completely based on components.
jQuery UI is a set of user interface interactions built on jQuery...
Python tutorial provides basic and advanced concepts of Python.
Java is an object-oriented, class-based computer-programming language.
Node.js is a cross-platform environment and library for running JavaScript app...
PHP is an open-source,interpreted scripting language.
Go is a programming language which is developed by Google...
C programming is considered as the base for other programming languages.
C++ is an object-oriented programming language. It is an extension to C programming.
C# is a programming language of .Net Framework.
Ruby is an open-source and fully object-oriented programming language.
JSP technology is used to create web application just like Servlet technology.
The JSTL represents a set of tags to simplify the JSP development.
ASP.NET is a web framework designed and developed by Microsoft.
Perl is a cross-platform environment and library for running JavaScript...
Scala is an object-oriented and functional programming language.
VBA stands for Visual Basic for Applications.
Spring is a lightweight framework.Spring framework makes ...
Spring Boot is a Spring module that provides the RAD feature...
Django is a Web Application Framework which is used to develop web applications.
Servlet technology is robust and scalable because of java language.
The Struts 2 framework is used to develop MVC based web applications.
Hibernate is an open source, lightweight, ORM tool.
Solr is a scalable, ready-to-deploy enterprise search engine.
SQL is used to perform operations on the records stored in the database.
MySQL is a relational database management system based...
Oracle is a relational database management system.
SQL Server is software developed by Microsoft.
PostgreSQL is an ORDBMS.
DB2 is a database server developed by IBM.
Redis is a No SQL database which works on the concept of key-value pair.
SQLite is embedded relational database management system.
MongoDB is a No SQL database. It is an document-oriented database...
Memcached is a free, distributed memory object caching system.
Hibernate is an open source, lightweight, ORM tool.
PL/SQL is a block structured language that can have multiple blocks in it.
DBMS Tutorial is software that is used to manage the database.
Spark is a unified analytics engine for large-scale data processing...
IntelliJ IDEA is an IDE for Java Developers which is developed by...
Git is a modern and widely used distributed version control system in the world.
GitHub is an immense platform for code hosting.
SVN is an open-source centralized version control system.
Maven is a powerful project management tool that is based on POM.
Jsoup is a java html parser.
UML is a general-purpose, graphical modeling language.
RESTful Web Services are REST Architecture based Web Services.
Postman is one testing tools which is used for API testing.
JMeter is to analyze the performance of web application.
Jenkins builds and tests our software projects.
SEO stands for Search Engine Optimization.
MATLAB is a software package for mathematical computation, visualization...
Unity is an engine for creating games on multiple platforms.
Hadoop is an open source framework.
Pig is a high-level data flow platform for executing Map Reduce programs of Hadoop.
Spark is a unified analytics engine for large-scale data processing...
Spring Cloud is a framework for building robust cloud applications.
Spring Boot is a Spring module that provides the RAD feature...
AI is one of the fascinating and universal fields of Computer.
Cloud computing is a virtualization-based technology.
AWS stands for Amazon Web Services which uses distributed IT...
Microsoft Azure is a cloud computing platform...
IoT stands for Internet of Things...
Spring Cloud is a framework for building robust cloud applications.
Email:jjw.quan@gmail.com