{"id":9541,"date":"2021-01-21T15:04:52","date_gmt":"2021-01-21T20:04:52","guid":{"rendered":"http:\/\/local.brightwhiz\/?p=9541"},"modified":"2021-12-04T05:34:10","modified_gmt":"2021-12-04T05:34:10","slug":"change-elements-class-with-javascript","status":"publish","type":"post","link":"http:\/\/local.brightwhiz\/change-elements-class-with-javascript\/","title":{"rendered":"How can I Change an HTML Element’s Class With JavaScript or JQuery?"},"content":{"rendered":"\n
In this practical HTML guide, we will show you how to change an HTML element’s class using Vanilla JavaScript<\/a> or JQuery with examples.<\/p>\n\n\n\n Web browsers allow users to add or remove classes attached to an element in response to mouse clicks, taps, or any other event.<\/p>\n\n\n\n The ability to change an HTML<\/a> Element’s class is a very important function in making web pages more interactive and dynamic. You can use it to change the element’s appearance or even behavior.<\/p>\n\n\n\n Modern browsers have a read-only property called classList<\/strong> that returns a live DOMTokenList<\/strong> collection of the class attributes of the element.<\/p>\n\n\n\n classList<\/strong> provides methods to conveniently add or remove classes as seen below:<\/p>\n\n\n\n <\/p>\n\n\n\n The above will not work in versions less than Internet Explorer 10.<\/p>\n\n\n\n You can also use className<\/strong> to replace all existing classes with one or more new classes.<\/p>\n\n\n\n <\/p>\n\n\n\n To preserve the existing classes while adding a new class to an element, use this:<\/p>\n\n\n\n <\/p>\n\n\n\n Remove a Single Class From an Element Using className:<\/strong><\/p>\n\n\n\n You can remove a single class from an element without affecting other classes if they exist using a regex replace seen below:<\/p>\n\n\n\n <\/p>\n\n\n\n Check if a Particular Class Exists:<\/strong><\/p>\n\n\n\n <\/p>\n\n\n\n You can also use JQuery<\/a>‘s simpler syntax to change the classes in an HTML element in case you intend to use the library in your project.<\/p>\n\n\n\n <\/p>\n\n\n\n There you have it. That is the way to change an element’s class in HTML using either vanilla JavaScript of the JQuery library.<\/p>\n\n\n\n Ref: [1<\/a>], [2<\/a>]<\/p>\n","protected":false},"excerpt":{"rendered":" In this practical HTML guide, we will show you how to change an HTML element’s class using Vanilla JavaScript or JQuery with examples. Web browsers allow users to add or…<\/p>\n","protected":false},"author":1,"featured_media":9542,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,23,16],"tags":[303,320,328,333,600,635,637],"yoast_head":"\nLet’s See How to Change an Element’s Class<\/h2>\n\n\n\n
\/\/ add a class\ndocument.getElementById("ElementID").classList.add('MyClass');\n\n\/\/ remove a class\ndocument.getElementById("ElementID").classList.remove('MyClass');\n\n\/\/ toggle a class\ndocument.getElementById("ElementID").classList.toggle('MyClass');\n\n\/\/ check if class is present\nif ( document.getElementById("ElementID").classList.contains('MyClass') );<\/code><\/pre>\n\n\n\n
Using className<\/h2>\n\n\n\n
\/\/ replaces all existing classes with MyClass\ndocument.getElementById("ElementID").className = "MyClass";<\/code><\/pre>\n\n\n\n
\/\/ Note the plus: (+=)\ndocument.getElementById("ElementID").className += " MyClass";<\/code><\/pre>\n\n\n\n
document.getElementById("ElementID").className =\n document.getElementById("ElementID").className.replace\n ( \/(?:^|\\s)MyClass(?!\\S)\/g , '' );<\/code><\/pre>\n\n\n\n
if ( document.getElementById("#ElementID").className.match(\/(?:^|\\s)MyClass(?!\\S)\/) )<\/code><\/pre>\n\n\n\n
Change Element’s Class with jQuery<\/h2>\n\n\n\n
\/\/ add the class\n$('#ElementID').addClass('MyClass');\n\n\/\/ remove the class\n$('#ElementID').removeClass('MyClass');\n\n\/\/ toggle the class in or out\n$('#ElementID').toggleClass('MyClass');\n\n\/\/ check if the class exists\nif ( $('#ElementID').hasClass('MyClass') )<\/code><\/pre>\n\n\n\n