{"id":5771,"date":"2024-12-09T07:27:51","date_gmt":"2024-12-09T07:27:51","guid":{"rendered":"https:\/\/ubiq.co\/tech-blog\/?p=5771"},"modified":"2024-12-09T07:27:53","modified_gmt":"2024-12-09T07:27:53","slug":"how-to-change-elements-class-with-javascript","status":"publish","type":"post","link":"https:\/\/ubiq.co\/tech-blog\/how-to-change-elements-class-with-javascript\/","title":{"rendered":"How to Change Element&#8217;s Class With JavaScript"},"content":{"rendered":"\n<p>Often web developers need to change the class name of one or more HTML elements on their web pages. This is because various JavaScript functions and CSS styles are attached to class names. So by changing class names of elements, developers can easily change the functionality &amp; appearance of their web pages. Sometimes, these kind of changes need to be triggered when events like button clicks are fired. In this article, we will learn how to change element&#8217;s class with JavaScript. You can use any of these methods.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-change-elements-class-with-javascript\/#How_to_Change_Elements_Class_With_JavaScript\" >How to Change Element&#8217;s Class With JavaScript<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-change-elements-class-with-javascript\/#1_HTML5_Solution\" >1. HTML5 Solution<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-change-elements-class-with-javascript\/#2_Set_All_Classes_for_Element\" >2. Set All Classes for Element<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-change-elements-class-with-javascript\/#3_Add_New_Class_to_Element\" >3. Add New Class to Element<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-change-elements-class-with-javascript\/#4_Remove_Class_From_Element\" >4. Remove Class From Element<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-change-elements-class-with-javascript\/#5_Check_if_Class_Exists\" >5. Check if Class Exists<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-change-elements-class-with-javascript\/#6_Change_Class_Using_jQuery\" >6. Change Class Using jQuery<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-change-elements-class-with-javascript\/#7_Change_class_of_multiple_elements\" >7. Change class of multiple elements<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-change-elements-class-with-javascript\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_to_Change_Elements_Class_With_JavaScript\"><\/span>How to Change Element&#8217;s Class With JavaScript<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>There are several ways to easily change element&#8217;s class with JavaScript. Let us look at them one by one. Let us say you have the following HTML element with id and class attributes already defined.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;div id='div_id' class='div_class'&gt;hello world&lt;\/div&gt;<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1_HTML5_Solution\"><\/span>1. HTML5 Solution<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Most modern web browsers such as Chrome, Firefox, Edge, Safari, Opera support <a href=\"https:\/\/en.wikipedia.org\/wiki\/HTML5\">HTML5<\/a>. In this version of HTML, each element already has a property classList that contains a list of all class names already assigned to the element. This classList property further supports methods like add(), remove(), contains() and toggle() to add, remove, check and toggle class names. Let us learn more about them.<\/p>\n\n\n\n<p>You can easily select an element using document.getElementById() function. We will use it to select the element and use its classList() property.<\/p>\n\n\n\n<p>Here is the code to select our div and add class &#8216;my_div&#8217; to it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">document.getElementById(\"div_id\").classList.add('my_div');<\/pre>\n\n\n\n<p>Once you run the above code, your div will have two classes div_class as well as my_div. If your div already has my_div class present, then it will not give any error or warning. Every element can have only unique class names so it will not once again the my_div class name either.<\/p>\n\n\n\n<p>Here is how to remove the class &#8216;my_div&#8217; from it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">document.getElementById(\"div_id\").classList.remove('my_div');<\/pre>\n\n\n\n<p>Once you run the above code, your div will only have class div_class and not my_div in it. If my_div class is not present and you run the above code, it will not give any error.<\/p>\n\n\n\n<p>If you want to check if an element contains a specific class name or not, then you can call contains() function.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">document.getElementById(\"div_id\").classList.contains('my_div');<\/pre>\n\n\n\n<p>The above code will return True or False depending on whether the class name is present or not. <\/p>\n\n\n\n<p>You can also toggle a class name for a given element using toggle() function.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">document.getElementById(\"div_id\").classList.toggle('my_div');<\/pre>\n\n\n\n<p>The above code will first check if the class my_div is present or not, and if present, it will remove the class. If it is not present, then it will add the class. In both cases, the other class names are left unchanged. This is generally useful to toggle the class name of an element on calling an event such as button click. Based on the toggling of class name of an element, you can change CSS style of the element.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;script type=\"text\/javascript\"&gt;<br>    function changeClass(){<br>        document.getElementById(\"div_id\").classList.toggle('my_div');<br>    }<br>&lt;\/script&gt;<br>...<br>&lt;button onClick=\"changeClass()\"&gt;My Button&lt;\/button&gt;<br>&lt;div id='div_id' class='div_class'&gt;hello world&lt;\/div&gt;<\/pre>\n\n\n\n<p>In this example, we have a button a div on web page. We have added a click event handler for the button. In this event handler, we basically toggle the class name of div.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2_Set_All_Classes_for_Element\"><\/span>2. Set All Classes for Element<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>All the above methods we have seen so far, work well on web browser versions that support HTML5. Some of the older web browsers such as IE may not support it. In such cases, you can use a more basic JavaScript code. The approach remains the same. First, we select the target element, then use a function or property setting to change its class name.<\/p>\n\n\n\n<p>Every HTML element supports a property className that you can use to set a class name to an element.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">document.getElementById(\"div_id\").className = 'my_div';<\/pre>\n\n\n\n<p>Please note, the above code will completely replace all existing classes of the element with my_div. If you want, you can also specify multiple class names in the above code to set one or more classes. Here is an example to set class name to &#8216;my_div&#8217; and &#8216;my_div2&#8217;. You need to mention them in a space-separated format.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">document.getElementById(\"div_id\").className = 'my_div my_div2';<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"3_Add_New_Class_to_Element\"><\/span>3. Add New Class to Element<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>If you want to simply add a new class name without changing any of the existing class names, use the following code.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">document.getElementById(\"div_id\").className += ' my_div';<\/pre>\n\n\n\n<p>Please note a couple of things above. Here we use += operator, instead of = operator, since we need to append a new class name, instead of overwriting existing class names. Secondly, in the string containing class name, we need to provide a space before the new class name (&#8216; my_div&#8217; instead of &#8216;my_div&#8217;).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"4_Remove_Class_From_Element\"><\/span>4. Remove Class From Element<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>We need to use replace() function to remove a class name from an element. Here is the code to remove my_div class.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">document.getElementById(\"<strong>div_id<\/strong>\").className =   document.getElementById(\"<strong>div_id<\/strong>\").className.replace<br>      ( \/(?:^|\\s)<strong>my_div<\/strong>(?!\\S)\/g , '' )<\/pre>\n\n\n\n<p>In the replace function, we need to use a regular expression to match the class name and replace it with empty string. In the above code, replace div_id with the ID of the element whose class you want to remove. Replace my_div with the class name you want to remove.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"5_Check_if_Class_Exists\"><\/span>5. Check if Class Exists<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Sometimes, you simply need to check if an element contains a specific class. In this case, you can use the match() function as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">document.getElementById(\"<strong>div_id<\/strong>\").className.match(\/(?:^|\\s)<strong>my_div<\/strong>(?!\\S)\/)<\/pre>\n\n\n\n<p>The above code again uses a different regular expression. Replace my_div with the class name you want to check and replace div_id with the id of the element for whom you want to run this code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"6_Change_Class_Using_jQuery\"><\/span>6. Change Class Using jQuery<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You can also use popular jQuery library that makes it very easy to select an element and change its class names.<\/p>\n\n\n\n<p>Only you will need to include jQuery library file in your web page. jQuery provides addClass(), removeClass() and hasClass() functions to add new class, remove class and check if an element has a class respectively. Here is the code to add class &#8216;my_div&#8217; to element with id=div_id. You can select the div using $(&#8216;#div_id&#8217;) selector.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$('#div_id').addClass('my_div');<\/pre>\n\n\n\n<p>Here is the code to remove the class &#8216;my_div&#8217; from our div.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$('#div_id').removeClass('my_div');<\/pre>\n\n\n\n<p>Here is the code to check if the element has a class my_div.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$('#div_id').hasClass('my_div');<\/pre>\n\n\n\n<p>You can also use toggleClass() function to toggle the class of the element.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$('#div_id').toggleClass('my_div');<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"7_Change_class_of_multiple_elements\"><\/span>7. Change class of multiple elements<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>So far, we have seen all examples where we change the class of one element only. But what if you want to change class of multiple elements? You can easily do that by simply changing the selector. Here is the code to add class &#8216;my_div&#8217; to all elements with class &#8216;div_class&#8217; in jQuery.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$('.div_class').addClass('my_div');<\/pre>\n\n\n\n<p>The same code using plain JavaScript. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">var items = document.getElementsByClassName(\"div_class\");<br>for(var i=0; i &lt; items.length; i++){<br>   items[i].className+='my_div';<br>}<\/pre>\n\n\n\n<p>Here we need to use getElementsByClassName() function to obtain a collection of elements having a specific class name. Since it is an array-like result, we need to loop through its items and change the class name for each item. We cannot directly set their className property.<\/p>\n\n\n\n<p>The main difference between jQuery and JavaScript here is that jQuery functions automatically work on all items whether the CSS selector returns one or multiple items. So you can use the same functions and code for single or multiple elements. In case of JavaScript, you need to loop through the result, if your selection returns multiple items. So you need to change your approach depending on whether you target one or multiple elements.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In this article, we have learnt many different ways to change HTML element&#8217;s class in JavaScript as well as jQuery. You can use any of these methods depending on your requirement. Although we have provided solutions for a div, you can use it for all elements, since we are using CSS selectors. The key part is to correctly select the desired HTML element, using CSS selector. Thereafter, you can use any of the solutions to alter the element&#8217;s class name. Among them, using HTML5 solutions is the easiest, whereas if you also want to support old web browsers then go for the basic JavaScript solutions mentioned above. If your website is already using jQuery, then we recommend using jQuery functions since they are very easy to use and versatile.<\/p>\n\n\n\n<p>Also read:<\/p>\n\n\n\n<p><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-set-cellpadding-and-cellspacing-in-css\/\">How to Set Cellpadding &amp; Cellspacing in CSS<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-horizontally-center-div-in-css\/\">How to Horizontally Center Div in CSS<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-change-html-inputs-placeholder-color-with-css\/\">How to Change HTML Input&#8217;s Placeholder Color<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Often web developers need to change element&#8217;s class with JavaScript. Here are the steps to do this.<\/p>\n","protected":false},"author":1,"featured_media":5806,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[295],"tags":[345],"class_list":["post-5771","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript","tag-element-class"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Change Element&#039;s Class With JavaScript - Ubiq BI<\/title>\n<meta name=\"description\" content=\"Often web developers need to change element&#039;s class with JavaScript. Here is how to do this using HTML5, jQuery and plain JavaScript.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-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 to Change Element&#039;s Class With JavaScript - Ubiq BI\" \/>\n<meta property=\"og:description\" content=\"Often web developers need to change element&#039;s class with JavaScript. Here is how to do this using HTML5, jQuery and plain JavaScript.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/ubiq.co\/tech-blog\/how-to-change-elements-class-with-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"Ubiq BI\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ubiqbi\" \/>\n<meta property=\"article:published_time\" content=\"2024-12-09T07:27:51+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-12-09T07:27:53+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/change-html-element-class-name.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"308\" \/>\n\t<meta property=\"og:image:height\" content=\"204\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Sreeram Sreenivasan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@UbiqBI\" \/>\n<meta name=\"twitter:site\" content=\"@UbiqBI\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sreeram Sreenivasan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-change-elements-class-with-javascript\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-change-elements-class-with-javascript\\\/\"},\"author\":{\"name\":\"Sreeram Sreenivasan\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\"},\"headline\":\"How to Change Element&#8217;s Class With JavaScript\",\"datePublished\":\"2024-12-09T07:27:51+00:00\",\"dateModified\":\"2024-12-09T07:27:53+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-change-elements-class-with-javascript\\\/\"},\"wordCount\":1358,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-change-elements-class-with-javascript\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/change-html-element-class-name.jpg?fit=308%2C204&ssl=1\",\"keywords\":[\"element class\"],\"articleSection\":[\"JavaScript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-change-elements-class-with-javascript\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-change-elements-class-with-javascript\\\/\",\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-change-elements-class-with-javascript\\\/\",\"name\":\"How to Change Element's Class With JavaScript - Ubiq BI\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-change-elements-class-with-javascript\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-change-elements-class-with-javascript\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/change-html-element-class-name.jpg?fit=308%2C204&ssl=1\",\"datePublished\":\"2024-12-09T07:27:51+00:00\",\"dateModified\":\"2024-12-09T07:27:53+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\"},\"description\":\"Often web developers need to change element's class with JavaScript. Here is how to do this using HTML5, jQuery and plain JavaScript.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-change-elements-class-with-javascript\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-change-elements-class-with-javascript\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-change-elements-class-with-javascript\\\/#primaryimage\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/change-html-element-class-name.jpg?fit=308%2C204&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/change-html-element-class-name.jpg?fit=308%2C204&ssl=1\",\"width\":308,\"height\":204,\"caption\":\"change html element's class name\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-change-elements-class-with-javascript\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Change Element&#8217;s Class With JavaScript\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#website\",\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/\",\"name\":\"Ubiq BI\",\"description\":\"Build dashboards &amp; reports in minutes\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\",\"name\":\"Sreeram Sreenivasan\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"caption\":\"Sreeram Sreenivasan\"},\"description\":\"Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI &amp; software development.\",\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/sreeram-sreenivasan\\\/\"],\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/author\\\/wordpress\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Change Element's Class With JavaScript - Ubiq BI","description":"Often web developers need to change element's class with JavaScript. Here is how to do this using HTML5, jQuery and plain JavaScript.","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":"https:\/\/ubiq.co\/tech-blog\/how-to-change-elements-class-with-javascript\/","og_locale":"en_US","og_type":"article","og_title":"How to Change Element's Class With JavaScript - Ubiq BI","og_description":"Often web developers need to change element's class with JavaScript. Here is how to do this using HTML5, jQuery and plain JavaScript.","og_url":"https:\/\/ubiq.co\/tech-blog\/how-to-change-elements-class-with-javascript\/","og_site_name":"Ubiq BI","article_publisher":"https:\/\/www.facebook.com\/ubiqbi","article_published_time":"2024-12-09T07:27:51+00:00","article_modified_time":"2024-12-09T07:27:53+00:00","og_image":[{"width":308,"height":204,"url":"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/change-html-element-class-name.jpg","type":"image\/jpeg"}],"author":"Sreeram Sreenivasan","twitter_card":"summary_large_image","twitter_creator":"@UbiqBI","twitter_site":"@UbiqBI","twitter_misc":{"Written by":"Sreeram Sreenivasan","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-change-elements-class-with-javascript\/#article","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-change-elements-class-with-javascript\/"},"author":{"name":"Sreeram Sreenivasan","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"headline":"How to Change Element&#8217;s Class With JavaScript","datePublished":"2024-12-09T07:27:51+00:00","dateModified":"2024-12-09T07:27:53+00:00","mainEntityOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-change-elements-class-with-javascript\/"},"wordCount":1358,"commentCount":0,"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-change-elements-class-with-javascript\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/change-html-element-class-name.jpg?fit=308%2C204&ssl=1","keywords":["element class"],"articleSection":["JavaScript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/ubiq.co\/tech-blog\/how-to-change-elements-class-with-javascript\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-change-elements-class-with-javascript\/","url":"https:\/\/ubiq.co\/tech-blog\/how-to-change-elements-class-with-javascript\/","name":"How to Change Element's Class With JavaScript - Ubiq BI","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-change-elements-class-with-javascript\/#primaryimage"},"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-change-elements-class-with-javascript\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/change-html-element-class-name.jpg?fit=308%2C204&ssl=1","datePublished":"2024-12-09T07:27:51+00:00","dateModified":"2024-12-09T07:27:53+00:00","author":{"@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"description":"Often web developers need to change element's class with JavaScript. Here is how to do this using HTML5, jQuery and plain JavaScript.","breadcrumb":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-change-elements-class-with-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/ubiq.co\/tech-blog\/how-to-change-elements-class-with-javascript\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-change-elements-class-with-javascript\/#primaryimage","url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/change-html-element-class-name.jpg?fit=308%2C204&ssl=1","contentUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/change-html-element-class-name.jpg?fit=308%2C204&ssl=1","width":308,"height":204,"caption":"change html element's class name"},{"@type":"BreadcrumbList","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-change-elements-class-with-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/ubiq.co\/tech-blog\/"},{"@type":"ListItem","position":2,"name":"How to Change Element&#8217;s Class With JavaScript"}]},{"@type":"WebSite","@id":"https:\/\/ubiq.co\/tech-blog\/#website","url":"https:\/\/ubiq.co\/tech-blog\/","name":"Ubiq BI","description":"Build dashboards &amp; reports in minutes","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ubiq.co\/tech-blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc","name":"Sreeram Sreenivasan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","caption":"Sreeram Sreenivasan"},"description":"Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI &amp; software development.","sameAs":["https:\/\/www.linkedin.com\/in\/sreeram-sreenivasan\/"],"url":"https:\/\/ubiq.co\/tech-blog\/author\/wordpress\/"}]}},"jetpack_featured_media_url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/change-html-element-class-name.jpg?fit=308%2C204&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/pbGGTT-1v5","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/5771","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/comments?post=5771"}],"version-history":[{"count":45,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/5771\/revisions"}],"predecessor-version":[{"id":5817,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/5771\/revisions\/5817"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media\/5806"}],"wp:attachment":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media?parent=5771"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/categories?post=5771"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/tags?post=5771"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}