Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically change CSS class of an HTML tag?

I am using JavaScript. I have a variable var boolVal that either evaluates to true/false. On my page, I have a <div> tag:

<div id='div1' class="redClass"></div> 

Based on the value of var boolVal, I want to change the CSS class of the <div> tag to blueClass.

For example: present class makes <div> color red, then the new class should make the page blue at runtime without need for page refresh.

Can we achieve this in simple JavaScript? Can we use

document.getElementById("MyElement").className = "MyClass"; 

or should we use AddClass?

like image 223
variable Avatar asked Mar 22 '14 11:03

variable


People also ask

How do you dynamically change a class in CSS style?

To do the first thing I use $(". className"). css() method, but it changes style only for those elements that already have className class, i.e. if I later add className to an element its style won't be new.

Can you dynamically change CSS?

If you want to change the CSS styles dynamically you'll have to attach this portion of code to some event. For example, if you want to change the styles of your element when clicking on a button, then you have to first listen to the click event and attach a function containing the previous code.

How do I change a specific class in CSS?

To select elements with a specific class, write a period (.) character, followed by the name of the class. You can also specify that only specific HTML elements should be affected by a class.

How to dynamically create a CSS class and apply to element?

In this article, we will see how to dynamically create a CSS class and apply to the element dynamically using JavaScript. To do that, first we create a class and assign it to HTML elements on which we want to apply CSS property. We can use className and classList property in JavaScript. The className property used to add a class in JavaScript.

How to change the CSS styles of an HTML element?

For such scenarios, we may need to rely on JavaScript or jQuery for changing the CSS styles of an HTML element. To change an HTML element’s style, say a <div>, we should select the <div> or that particular HTML element uniquely.

How to interchange two CSS classes dynamically?

These classes get interchanged by simply clicking on the done. This functionality is achieved by the javascript function. The two css classes look like : Now we need to define the logic in javascript which let us interchange the two classes dynamically.

How to add dynamic HTML to a Div in HTML5?

You want to add dynamic HTML to this div. First, you need to create the element you want to insert using createElement () method. Here, I have created a paragraph <p> element using createElement () method and stored it in a constant p.


2 Answers

You can add a CSS class based on id dynamically using classList API as follows:

document.getElementById('idOfElement').classList.add('newClassName'); 

Or the old way:

document.getElementById('idOfElement').className = 'newClassName'; // += to keep existing classes 

Alternatively you can use other DOM query methods shown below to find elements. The last three return a collection so you'll have to iterate over it and apply the class to each element in the collection (similar to the example given below each).

  • querySelector

  • querySelectorAll

    elements.forEach(element => element.classList.add('newClassName')); 
  • getElementsByClassName

    Array.from(elements).forEach(element => element.classList.add('newName')); 
  • getElementsByTagName

    Array.from(elements).forEach(element => element.classList.add('newName')); 

In your case

var element = document.getElementById('div1'); if(boolVal)    element.className= 'blueClass'; // += ' blueClass'; to keep existing classes else    element.className= 'redClass'; 
like image 65
T J Avatar answered Sep 22 '22 07:09

T J


First of all, AddClass is not a pure Javascript method. It's related to jQuery.

You can use Javascript for adding a class:

setAttribute and className both method are used to set class (class attribute), these method are not used to adding another class with existing one.

document.getElementById('div1').setAttribute( "class", "blueClass" ); 

OR

document.getElementById('div1').className="redClass"; 

Demo Fiddle

So if you want append a css class to an element, you can do it like this -

 document.getElementById('div1').setAttribute( "class", document.getElementById('div1').getAttribute('class') + " blueClass" ); 

OR

 document.getElementById('div1').className +=" redClass"; 

Note: Using this way, the same class can be added multiple times. It's only a trick to do this work using javascript, it's not a perfect solution

OR simply use jQuery to add class -

$("#div1").addClass("blueClass"); 

Working Fiddle

like image 38
Ishan Jain Avatar answered Sep 23 '22 07:09

Ishan Jain