Javascript - Add class to a class (not ID)

javascript add class to div
remove class javascript
javascript change css class property
javascript remove class from all elements
addclass jquery
css add class to element
jquery remove class
javascript toggle class

In a HTML file i load a javascript file (in the header) which contains some code. In that code it checks if a cookie exists. If the cookie doesn't exists it should add a class to a div which should be matched with a class.

Example:

The class where it should add the class:

<header class="firstClass">

The javascript:

var classcode = document.getElementsByClassName("firstClass");

if (myCookie == null) {
    add class "secondClass" to "firstClass";
}

I've tried several things but I can't get it to work...

I should mention, the javascript file is loaded before the div where the class should be added.


document.getElementsByClassName returns a collection. Use spread operator to convert it into array and use array methods(forEach) in this case. Then check if the array length is greater than 0, if greater than 0 then add another class using classList.add

var classcode = [...document.getElementsByClassName("firstClass")];
if (classcode.length > 0) {
  classcode.forEach(function(item) {
    item.classList.add('anotherClass')
  })
}
.anotherClass {
  color: green;
}
<header class="firstClass">Header Here</header>
<div class="someOtherClass"> Some Text</div>
<div class="firstClass">Another Text</div>

How To Add a Class Name, Learn how to add a class name to an element with JavaScript. Add a class name to the div element with id="myDIV" (in this example we use a button to add the class). Note: The classList property is not supported in Internet Explorer 9. The addClass() method adds one or more class names to the selected elements. This method does not remove existing class attributes, it only adds one or more class names to the class attribute. Tip: To add more than one class, separate the class names with spaces.


You could use querySelectorAll, in order to find all the elements with the class firstClass. This will result in an array, then you just have to loop through it.

var classcode = document.querySelectorAll(".firstClass");

classcode.forEach(function(element) {
    element.classList.add("mystyle")
  console.log(element);
});

jQuery addClass() Method, This method does not remove existing class attributes, it only adds one or more class names to the class attribute. Tip: To add more than one class, separate the​  I would like that "p" I just created have an unique identifier (ID) and set the name of the ID. So later on when the user wants to delete the last created element it will be able to get the ID so it can removeChild.


You need to use classList.add() to each of the elements that has class firstClass. Although you might have single element, but it is better to use a loop for selecting each element so that in future when you add element in HTML with this same class then you do not need to change the JS code.

var classcode = document.getElementsByClassName("firstClass");

for(var i=0; i<classcode.length; i++){
   classcode[i].classList.add('secondClass');
}
.secondClass{
  color: green;
}
<div class="firstClass">1</div>
<div class="firstClass">2</div>
<div class="firstClass">3</div>

Quick Tip: Add or Remove a CSS Class with Vanilla JavaScript , Sometimes you need to add or remove a CSS class with JavaScript, and you elements for (var i=0; i<elements.length; i++) { // if class is not already <div id="​iddiv" class="highlight">ID div</div> <div class="classdiv">Class  Methods. The constructor method is special, it is where you initialize properties, it is called automatically when a class is initiated, and it has to have the exact name "constructor", in fact, if you do not have a constructor method, JavaScript will add an invisible and empty constructor method. You are also free to make your own methods,


Try this:-

var classcode = document.getElementsByClassName("firstClass");
if (myCookie == null) {
    classcode .classList.add("secondClass");
}

What is a reference ID ? Where do you find it on the dashboard , How can the style class of an element be changed in JavaScript? element.setAttribute (name, value); Replace name with class. Replace value with whatever name you have given the class, enclosed in quotes. This avoids needing to delete the current class and adding a different one. This jsFiddle example shows full working code. – Alan Wells May 18 '14 at 4:59. For changing a class of HTML element with


I think all solutions will work. The problem was that I had to load the javascript file after the div which should get the class added. Thanks al for the help! :)

Dynamic style - manipulating CSS with JavaScript, Hi, I'm working with a CMS where it's not possible to add an ID to the label. JQuery is available, but I'd like to learn how to do it both JQuery and  Note: The classList property is not supported in Internet Explorer 9. The following code will work in all browsers: The following code will work in all browsers: Example


How to add class to a label without an ID? - JavaScript, It's important to note that this method does not replace a class. It simply addClass() method manipulated the className property of the selected elements, not the class attribute. <script src="https://code.jquery.com/jquery-3.4​.1.js"></script>. It's important to note that this method does not replace a class. It simply adds the class, appending it to any which may already be assigned to the elements. Before jQuery version 1.12/2.2, the .addClass() method manipulated the className property of the selected elements, not the class attribute .


.addClass(), Here's few different method to add and remove classes from DOM elements, with If class already exists in the element's list of classes, it will not add the class  You can add id or class attribute using JavaScript like the following way, [code]document.getElementsByTagName("H1")[0].setAttribute("class", ";myclass&quot


JavaScript: adding and removing class names from elements , If you want to add a class to an HTML element without replacing its existing class, you can do something as shown in the following example: <title>Add Class to an Element Using JavaScript</title> Select div element by its id attribute Note: The classList property is not supported in Internet Explorer 9. The following code will work in all browsers: The following code will work in all browsers: Example