JavaScript Selecting and modifying DOM elements

How do I select DOM elements in JS? What is the equivalent for jQuery's $ selection syntax?

For example I have a <div> element:

<div id="idDiv">Div Element</div>

Now I want to apply addClass("ClassName") jQuery function on div. I can do it with jQuery with the following way:

$("#idDiv").addClass("ClassName") or jQuery("#idDiv").addClass("ClassName")

How can I do this with vanilla JS?

Answer:1

You can use the classList API:

// Adding classes
document.getElementById('idDiv').classList.add('foo');
// Toggling classes
document.getElementById('idDiv').classList.toggle('foo');
// Removing classes
document.getElementById('idDiv').classList.remove('bar');

Please note that IE9 and below do not support the API, for supporting those browsers you can use a shim, MDN has one.

An experimental solution:

function jFoo(selector) {
    return {
        elems: [].slice.call(document.querySelectorAll(selector)),
        _handleClass: function (cls, m) {
            var len = this.elems.length,
                cls = cls.trim().split(/\s/),
                clen = cls.length;
            for (var i = 0; i < len; i++) {
                for (var j = 0; j < clen; j++)
                this.elems[i].classList[m](cls[j]);
            }
            return this;
        },
        addClass: function (cls) {
           return this._handleClass(cls, 'add');
        },
        toggleClass: function (cls) {
           return this._handleClass(cls, 'toggle');
        },
        removeClass: function (cls) {
           return this._handleClass(cls, 'remove');
        },
    }
}

jFoo('selector').toggleClass('foo bar')
                .addClass('barz fool')
                .removeClass('foo'); 
Answer:2

You can get element by javascript in following way:

    var getelem = document.getElementById("idDiv");

    getelem.setAttribute("class", "active");
Answer:3

I'm currently making a game, and I'm trying to change the CSS values of a whole class of objects. For a single ID, I would use, document.getElementById("idHere"), but I need something like document....

I'm currently making a game, and I'm trying to change the CSS values of a whole class of objects. For a single ID, I would use, document.getElementById("idHere"), but I need something like document....

I have the following code jQuery $(".col-md-12").hide(); $(".button-div").hide(); $(".featurette-divider").hide(); $(".footer").hide(); $(".first").fadeIn(1000); $(".second").delay(900).fadeIn(1000); ...

I have the following code jQuery $(".col-md-12").hide(); $(".button-div").hide(); $(".featurette-divider").hide(); $(".footer").hide(); $(".first").fadeIn(1000); $(".second").delay(900).fadeIn(1000); ...

I am trying to add some obstacles to the canvas game that I've got but something seems to be wrong and I can't seem to put my finger on it. I just want some simple walls here and there to make the ...

I am trying to add some obstacles to the canvas game that I've got but something seems to be wrong and I can't seem to put my finger on it. I just want some simple walls here and there to make the ...

I am trying to use this example as shown HERE in JSFIDDLE, the problem i am having is that I would like when dropping to squeeze the new record in between and not swap. I tried adding a for loop in ...

I am trying to use this example as shown HERE in JSFIDDLE, the problem i am having is that I would like when dropping to squeeze the new record in between and not swap. I tried adding a for loop in ...