classList is a newly introduced attribute in HTML5 that returns the DOMTokenList of a DOM element. It’s a read-only list that contains all the classes of an element.
Let’s give it a try! Suppose we have a div element with three classes: “apple,” “pen,” and “pineapple.”
<div id="testDiv" class=" apple pen pineapple">
classList test
</div>
⬆️You can see that it has printed out the three classes. Now, let’s move on to discuss how to manipulate these classes.
Supported Methods
The classList attribute offers several functions for manipulating the class list. These include:
add: Adds a new class.
remove: Removes a class.
replace: Replaces a class with another.
toggle: Toggles a class – removes it if present, adds it if absent.
// Continuing from the example above
tarDiv.classList.add('orange') // Add a new class - orange
tarDiv.classList.remove('apple') // Remove apple
tarDiv.classList.replace('pen','pencil') // Replace with pencil
tarDiv.classList.toggle('pineapple') // Toggle - remove if present
Everyone can follow the instructions and try to imagine how the results should look like.
⬇ Print the results step by step and see if they match your expectations.
There are also functions for checking:
contains: Returns a boolean indicating whether a specific class is present.
tarDiv.classList.contains('apple')
Multiple Parameters – Handling Multiple Classes and Forcing
Both add and remove can accept multiple class names for operation, separated by commas.
toggle also accepts a second parameter – force, which is a boolean value.
As the name suggests, when force is set to true, the operation will be enforced regardless of whether the class exists or not. When set to false, it behaves in the opposite way – classes will always be removed.
// Forcefully adding a class
tarDiv.classList.toggle('apple', true); // Adding the 'apple' class back, even if it already exists
// Forcefully removing a class
tarDiv.classList.toggle('apple', false); // Removing the 'apple' class, even if it doesn't exist anymore
Conclusion
In conclusion, the classList attribute in HTML5 provides a convenient way to interact with an element’s classes.
You can easily manipulate classes and make dynamic changes to your webpage’s styling and behavior.
🧡 Hopefully, this post has helped you to some extent.
🧡You can support me by clicking some ad, Thanks a lot
✅If you got any problem about the explanation, please feel free to let me know
var uagb_data = {"ajax_url":"https:\/\/zyrastory.com\/wp-admin\/admin-ajax.php","uagb_masonry_ajax_nonce":"dd77efd820"};
var uagb_data = {"ajax_url":"https:\/\/zyrastory.com\/wp-admin\/admin-ajax.php","uagb_masonry_ajax_nonce":"dd77efd820","uagb_grid_ajax_nonce":"99af30cf4d"};
document.addEventListener("DOMContentLoaded", function(){
( function( $ ) {
var cols = parseInt( '3' );
var $scope = $( '.uagb-block-8f50cc49' );
let imagePosition = 'top';if( 'top' !== imagePosition ){
// This CSS is for Post BG Image Spacing
let articles = document.querySelectorAll( '.uagb-post__image-position-background .uagb-post__inner-wrap' );
if( articles.length ) {
for( let article of articles ) {
let image = article.getElementsByClassName('uagb-post__image');
if ( image[0] ) {
let articleWidth = article.offsetWidth;
let rowGap = 20;
let imageWidth = 100 - ( rowGap / articleWidth ) * 100;
image[0].style.width = imageWidth + '%';
image[0].style.marginLeft = rowGap / 2 + 'px';
}
}
}
}
// If this is not a Post Carousel, return.
// Else if it is a carousel but has less posts than the number of columns, return after setting visibility.
if ( ! $scope.hasClass('is-carousel') ) {
return;
} else if ( cols >= $scope.children('article.uagb-post__inner-wrap').length ) {
$scope.css( 'visibility', 'visible' );
return;
}
var slider_options = {
'slidesToShow' : cols,
'slidesToScroll' : 1,
'autoplaySpeed' : 2000,
'autoplay' : Boolean( '1' ),
'infinite' : Boolean( '1' ),
'pauseOnHover' : Boolean( '1' ),
'speed' : 500,
'arrows' : Boolean( '1' ),
'dots' : Boolean( '1' ),
'rtl' : Boolean( '' ),
'prevArrow' : '<button type=\"button\" data-role=\"none\" class=\"slick-prev\" aria-label=\"Previous\" tabindex=\"0\" role=\"button\"><svg width=\"20\" height=\"20\" viewBox=\"0 0 256 512\"><path d=\"M31.7 239l136-136c9.4-9.4 24.6-9.4 33.9 0l22.6 22.6c9.4 9.4 9.4 24.6 0 33.9L127.9 256l96.4 96.4c9.4 9.4 9.4 24.6 0 33.9L201.7 409c-9.4 9.4-24.6 9.4-33.9 0l-136-136c-9.5-9.4-9.5-24.6-.1-34z\"></path></svg><\/button>',
'nextArrow' : '<button type=\"button\" data-role=\"none\" class=\"slick-next\" aria-label=\"Next\" tabindex=\"0\" role=\"button\"><svg width=\"20\" height=\"20\" viewBox=\"0 0 256 512\"><path d=\"M224.3 273l-136 136c-9.4 9.4-24.6 9.4-33.9 0l-22.6-22.6c-9.4-9.4-9.4-24.6 0-33.9l96.4-96.4-96.4-96.4c-9.4-9.4-9.4-24.6 0-33.9L54.3 103c9.4-9.4 24.6-9.4 33.9 0l136 136c9.5 9.4 9.5 24.6.1 34z\"></path></svg><\/button>',
'responsive' : [
{
'breakpoint' : 1024,
'settings' : {
'slidesToShow' : 2,
'slidesToScroll' : 1,
}
},
{
'breakpoint' : 767,
'settings' : {
'slidesToShow' : 1,
'slidesToScroll' : 1,
}
}
]
};$scope.imagesLoaded( function() {
$scope.slick( slider_options );
}).always( function() {
$scope.css( 'visibility', 'visible' );
} );var enableEqualHeight = ( '1' );if( enableEqualHeight ){
$scope.imagesLoaded( function() {
UAGBPostCarousel?._setHeight( $scope );
});$scope.on( 'afterChange', function() {
UAGBPostCarousel?._setHeight( $scope );
} );
}} )( jQuery );
});
ai_front = {"insertion_before":"BEFORE","insertion_after":"AFTER","insertion_prepend":"PREPEND CONTENT","insertion_append":"APPEND CONTENT","insertion_replace_content":"REPLACE CONTENT","insertion_replace_element":"REPLACE ELEMENT","visible":"VISIBLE","hidden":"HIDDEN","fallback":"FALLBACK","automatically_placed":"Automatically placed by AdSense Auto ads code","cancel":"Cancel","use":"Use","add":"Add","parent":"Parent","cancel_element_selection":"Cancel element selection","select_parent_element":"Select parent element","css_selector":"CSS selector","use_current_selector":"Use current selector","element":"ELEMENT","path":"PATH","selector":"SELECTOR"};