{"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

Let’s See How to Change an Element’s Class<\/h2>\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

\/\/ 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

<\/p>\n\n\n\n

The above will not work in versions less than Internet Explorer 10.<\/p>\n\n\n\n

Using className<\/h2>\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

\/\/ replaces all existing classes with MyClass\ndocument.getElementById("ElementID").className = "MyClass";<\/code><\/pre>\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

\/\/ Note the plus: (+=)\ndocument.getElementById("ElementID").className += " MyClass";<\/code><\/pre>\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

document.getElementById("ElementID").className =\n   document.getElementById("ElementID").className.replace\n      ( \/(?:^|\\s)MyClass(?!\\S)\/g , '' );<\/code><\/pre>\n\n\n\n

<\/p>\n\n\n\n

Check if a Particular Class Exists:<\/strong><\/p>\n\n\n\n

if ( document.getElementById("#ElementID").className.match(\/(?:^|\\s)MyClass(?!\\S)\/) )<\/code><\/pre>\n\n\n\n

<\/p>\n\n\n\n

Change Element’s Class with jQuery<\/h2>\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

\/\/ 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

<\/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":"\nHow can I Change an HTML Element's Class With JavaScript or JQuery?<\/title>\n<meta name=\"description\" content=\"In this practical HTML guide we will show you how to change an HTML element's class using Vanilla JavaScript or JQuery with examples\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/local.brightwhiz\/change-elements-class-with-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How can I Change an HTML Element's Class With JavaScript or JQuery?\" \/>\n<meta property=\"og:description\" content=\"In this practical HTML guide we will show you how to change an HTML element's class using Vanilla JavaScript or JQuery with examples\" \/>\n<meta property=\"og:url\" content=\"http:\/\/local.brightwhiz\/change-elements-class-with-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"Brightwhiz.com\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/brightwhiz\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-01-21T20:04:52+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-12-04T05:34:10+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/01\/Change-Elements-Class.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Michael Bright\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@brightwhizmag\" \/>\n<meta name=\"twitter:site\" content=\"@brightwhizmag\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Michael Bright\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\/\/local.brightwhiz\/change-elements-class-with-javascript\/#article\",\"isPartOf\":{\"@id\":\"http:\/\/local.brightwhiz\/change-elements-class-with-javascript\/\"},\"author\":{\"name\":\"Michael Bright\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32\"},\"headline\":\"How can I Change an HTML Element’s Class With JavaScript or JQuery?\",\"datePublished\":\"2021-01-21T20:04:52+00:00\",\"dateModified\":\"2021-12-04T05:34:10+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/local.brightwhiz\/change-elements-class-with-javascript\/\"},\"wordCount\":277,\"publisher\":{\"@id\":\"http:\/\/local.brightwhiz\/#organization\"},\"image\":{\"@id\":\"http:\/\/local.brightwhiz\/change-elements-class-with-javascript\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/01\/Change-Elements-Class.jpg\",\"keywords\":[\"HTML\",\"Internet\",\"JavaScript\",\"jQuery\",\"Ui\/Ux\",\"Web\",\"Web Design\"],\"articleSection\":[\"Articles\",\"Guides\",\"Technology\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/local.brightwhiz\/change-elements-class-with-javascript\/\",\"url\":\"http:\/\/local.brightwhiz\/change-elements-class-with-javascript\/\",\"name\":\"How can I Change an HTML Element's Class With JavaScript or JQuery?\",\"isPartOf\":{\"@id\":\"http:\/\/local.brightwhiz\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/local.brightwhiz\/change-elements-class-with-javascript\/#primaryimage\"},\"image\":{\"@id\":\"http:\/\/local.brightwhiz\/change-elements-class-with-javascript\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/01\/Change-Elements-Class.jpg\",\"datePublished\":\"2021-01-21T20:04:52+00:00\",\"dateModified\":\"2021-12-04T05:34:10+00:00\",\"description\":\"In this practical HTML guide we will show you how to change an HTML element's class using Vanilla JavaScript or JQuery with examples\",\"breadcrumb\":{\"@id\":\"http:\/\/local.brightwhiz\/change-elements-class-with-javascript\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/local.brightwhiz\/change-elements-class-with-javascript\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/local.brightwhiz\/change-elements-class-with-javascript\/#primaryimage\",\"url\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/01\/Change-Elements-Class.jpg\",\"contentUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/01\/Change-Elements-Class.jpg\",\"width\":1200,\"height\":630,\"caption\":\"Change Element's Class\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/local.brightwhiz\/change-elements-class-with-javascript\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/local.brightwhiz\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How can I Change an HTML Element’s Class With JavaScript or JQuery?\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/local.brightwhiz\/#website\",\"url\":\"http:\/\/local.brightwhiz\/\",\"name\":\"Brightwhiz.com\",\"description\":\"Best Tech guides, Tutorials, and News\",\"publisher\":{\"@id\":\"http:\/\/local.brightwhiz\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/local.brightwhiz\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/local.brightwhiz\/#organization\",\"name\":\"Brightwhiz\",\"url\":\"http:\/\/local.brightwhiz\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png\",\"contentUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png\",\"width\":706,\"height\":135,\"caption\":\"Brightwhiz\"},\"image\":{\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/brightwhiz\/\",\"https:\/\/x.com\/brightwhizmag\",\"https:\/\/instagram.com\/bright_whiz\/\",\"https:\/\/www.pinterest.com\/sobbayi\/\",\"https:\/\/www.youtube.com\/channel\/UC6sCdP_d_RiTIM7ErFT-PSQ\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32\",\"name\":\"Michael Bright\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g\",\"contentUrl\":\"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g\",\"caption\":\"Michael Bright\"},\"sameAs\":[\"https:\/\/sobbayi.com\"],\"url\":\"http:\/\/local.brightwhiz\/author\/sobbayiadmin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How can I Change an HTML Element's Class With JavaScript or JQuery?","description":"In this practical HTML guide we will show you how to change an HTML element's class using Vanilla JavaScript or JQuery with examples","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/local.brightwhiz\/change-elements-class-with-javascript\/","og_locale":"en_US","og_type":"article","og_title":"How can I Change an HTML Element's Class With JavaScript or JQuery?","og_description":"In this practical HTML guide we will show you how to change an HTML element's class using Vanilla JavaScript or JQuery with examples","og_url":"http:\/\/local.brightwhiz\/change-elements-class-with-javascript\/","og_site_name":"Brightwhiz.com","article_publisher":"https:\/\/www.facebook.com\/brightwhiz\/","article_published_time":"2021-01-21T20:04:52+00:00","article_modified_time":"2021-12-04T05:34:10+00:00","og_image":[{"width":1200,"height":630,"url":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/01\/Change-Elements-Class.jpg","type":"image\/jpeg"}],"author":"Michael Bright","twitter_card":"summary_large_image","twitter_creator":"@brightwhizmag","twitter_site":"@brightwhizmag","twitter_misc":{"Written by":"Michael Bright","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/local.brightwhiz\/change-elements-class-with-javascript\/#article","isPartOf":{"@id":"http:\/\/local.brightwhiz\/change-elements-class-with-javascript\/"},"author":{"name":"Michael Bright","@id":"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32"},"headline":"How can I Change an HTML Element’s Class With JavaScript or JQuery?","datePublished":"2021-01-21T20:04:52+00:00","dateModified":"2021-12-04T05:34:10+00:00","mainEntityOfPage":{"@id":"http:\/\/local.brightwhiz\/change-elements-class-with-javascript\/"},"wordCount":277,"publisher":{"@id":"http:\/\/local.brightwhiz\/#organization"},"image":{"@id":"http:\/\/local.brightwhiz\/change-elements-class-with-javascript\/#primaryimage"},"thumbnailUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/01\/Change-Elements-Class.jpg","keywords":["HTML","Internet","JavaScript","jQuery","Ui\/Ux","Web","Web Design"],"articleSection":["Articles","Guides","Technology"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"http:\/\/local.brightwhiz\/change-elements-class-with-javascript\/","url":"http:\/\/local.brightwhiz\/change-elements-class-with-javascript\/","name":"How can I Change an HTML Element's Class With JavaScript or JQuery?","isPartOf":{"@id":"http:\/\/local.brightwhiz\/#website"},"primaryImageOfPage":{"@id":"http:\/\/local.brightwhiz\/change-elements-class-with-javascript\/#primaryimage"},"image":{"@id":"http:\/\/local.brightwhiz\/change-elements-class-with-javascript\/#primaryimage"},"thumbnailUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/01\/Change-Elements-Class.jpg","datePublished":"2021-01-21T20:04:52+00:00","dateModified":"2021-12-04T05:34:10+00:00","description":"In this practical HTML guide we will show you how to change an HTML element's class using Vanilla JavaScript or JQuery with examples","breadcrumb":{"@id":"http:\/\/local.brightwhiz\/change-elements-class-with-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/local.brightwhiz\/change-elements-class-with-javascript\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/local.brightwhiz\/change-elements-class-with-javascript\/#primaryimage","url":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/01\/Change-Elements-Class.jpg","contentUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/01\/Change-Elements-Class.jpg","width":1200,"height":630,"caption":"Change Element's Class"},{"@type":"BreadcrumbList","@id":"http:\/\/local.brightwhiz\/change-elements-class-with-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/local.brightwhiz\/"},{"@type":"ListItem","position":2,"name":"How can I Change an HTML Element’s Class With JavaScript or JQuery?"}]},{"@type":"WebSite","@id":"http:\/\/local.brightwhiz\/#website","url":"http:\/\/local.brightwhiz\/","name":"Brightwhiz.com","description":"Best Tech guides, Tutorials, and News","publisher":{"@id":"http:\/\/local.brightwhiz\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/local.brightwhiz\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/local.brightwhiz\/#organization","name":"Brightwhiz","url":"http:\/\/local.brightwhiz\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/","url":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png","contentUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png","width":706,"height":135,"caption":"Brightwhiz"},"image":{"@id":"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/brightwhiz\/","https:\/\/x.com\/brightwhizmag","https:\/\/instagram.com\/bright_whiz\/","https:\/\/www.pinterest.com\/sobbayi\/","https:\/\/www.youtube.com\/channel\/UC6sCdP_d_RiTIM7ErFT-PSQ"]},{"@type":"Person","@id":"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32","name":"Michael Bright","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/local.brightwhiz\/#\/schema\/person\/image\/","url":"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g","contentUrl":"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g","caption":"Michael Bright"},"sameAs":["https:\/\/sobbayi.com"],"url":"http:\/\/local.brightwhiz\/author\/sobbayiadmin\/"}]}},"_links":{"self":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/posts\/9541"}],"collection":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/comments?post=9541"}],"version-history":[{"count":0,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/posts\/9541\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/media\/9542"}],"wp:attachment":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/media?parent=9541"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/categories?post=9541"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/tags?post=9541"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}