13

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);
      }
    });
  }
});
2
  • 1
    Why the native js DOM functions and the jQuery tag? ^^ Commented Sep 15, 2015 at 12:59
  • @AmeyDeshpande jsfiddle.net/infous/8woaegx2 it's maybe a clear example of the code you want. Check results in DOM. Commented Sep 15, 2015 at 13:33

3 Answers 3

36

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');
}
Sign up to request clarification or add additional context in comments.

4 Comments

Not me. Maybe he is looking for pure js version.
@CrimsonChin where is it?
@Jai sadly it would require a time machine to prove, the user has since removed the tag.
Thank you for the raw js verion of this.... Rarely anybody provides raw js solutions... ;) +1.
5

In 2022 (with IE out of the equation), you would do this (using querySelectorAll, forEach, and an arrow function):

document.querySelectorAll('.foo').forEach(el=>el.classList.add('bar'));

1 Comment

One liner neat solution.
0

Sorry for coming late, but I was thinking that using getElementsByClassName is probably more efficient :

var myDivs = document.getElementsByClassName('model');
[].forEach.call(myDivs, function(mySingleDiv) {
    mySingleDiv.classList.add('newclass');
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.