Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a class to multiple elements?

Tags:

javascript

Basically I like to add a new class to multiple elements with the same class name. Currently it adds a class only to the last one. I have tried using document.querySelectorAll but got no results. What I'm missing?

HTML example

<div class="model"></div>
<div class="model"></div>
<div class="model"></div>

JS

 _updateParagraph: function(settings, className) {
    var paragraph = document.querySelector('.' + className);
    paragraph.className = className;
    this._updateParagraphClasslist(settings, paragraph);
  },

FULL JS

App.Views.Preview = Backbone.View.extend({
  el: "#preview",
  initialize: function() {
    this.listenTo(this.model, "change", this.update);
  },
  update: function() {
    var

      layout = [this.model.get("model")],
      format   = [this.model.get("size"), this.model.get("color-outside"), this.model.get("color")],
      color    = [this.model.get("color-inside")],
      material = [this.model.get('material')],
      lamination = [this.model.get('lamination')],
      logo = [this.model.get('embossing')];

    this._updateParagraph(layout, 'layout');
    this._updateParagraph(format, 'front');
    this._updateParagraph(format, 'back');
    this._updateParagraph(lamination, 'kit-front');
    this._updateParagraph(lamination, 'kit-back');
    this._updateParagraph(logo, 'embossing');
    this._updateParagraph(color, 'colorinner');
    this._updateParagraph(color, 'model');


  },
  _updateParagraph: function(settings, className) {
    var paragraph = document.querySelector('.' + className);
    paragraph.className = className;
    this._updateParagraphClasslist(settings, paragraph);
  },
  _updateParagraphClasslist: function(settings, paragraph) {
    _.each(settings, function(setting) {
      if (_.has(setting, "visual")) {
        paragraph.classList.add(setting.visual);
      }
    });
  }
});
like image 578
Alexander Hein Avatar asked Sep 15 '15 12:09

Alexander Hein


People also ask

Can you use the same class on multiple elements?

Multiple HTML elements can share the same class.

Can we use class name with multiple HTML elements?

HTML elements can have an id and/or class attribute. The id attribute assigns a name to the element it is applied to, and for valid markup, there can be only one element with that name. The class attribute assigns a class name to the element, and that name can be used on many elements within the page.

How do you add multiple classes in HTML?

To specify multiple classes, separate the class names with a space, e.g. <span class="left important">. This allows you to combine several CSS classes for one HTML element.

How do you append a class?

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.

How to add multiple classes to an element in Java?

We then used the classList.add method to add multiple classes to the element. If a class is already present on the element, it will be omitted. The add () method only adds classes that are not already contained in the element's class list.

How do I add a class to a jQuery element?

jQuery addClass() Method jQuery HTML/CSS Methods. Example. ... 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.

How to add a class name to a div element?

Step 1) Add HTML: Add a class name to the div element with id="myDIV" (in this example we use a button to add the class). Example <button onclick="myFunction()">Try it</button> <div id="myDIV"> This is a DIV element. </div> Step 2) Add CSS:

How to add or remove a class from a classlist?

On each iteration, we used the classList.add method to add a class to the element. If the class is already present on the element, it won't get added twice. You can pass as many classes to the add () method as necessary. Similarly, if you need to remove one or more classes, you can use the remove () method.


1 Answers

I like to add a new class to multiple elements with the same class name

Using jQuery, you can target all element with class model along with .addClass() for adding class to all of them:

$('.model').addClass('newclass')

A pure javascript solution can be like following:

var divs = document.querySelectorAll('.model');
for (var i = 0; i < divs.length; i++) {
    divs[i].classList.add('newclass');
}
like image 141
Milind Anantwar Avatar answered Sep 18 '22 13:09

Milind Anantwar