4

Currently in JavaScript, in order to add a class to an element I use:

var element = document.getElementById("element");

var classes;
if (element.className.split(" ").indexOf("class") < 0) {
    classes = element.className.split(" ");
    classes.push("class");
    element.className = classes.join(" ");
}

If I wanted to make that code a method like jQuery's addClass() like below:

addClass: function(string) {
    var classes;
    if (element.className.split(" ").indexOf("class") < 0) {
        classes = element.className.split(" ");
        classes.push("class");
        element.className = classes.join(" ");
    }
}

In which object's prototype should I add this method to work like this:

document.getElementById("element").onclick = function() {
    this.addClass("class");
}
4
  • 5
    Extending builtins' prototypes with nonstandard functions can make for confusing code. I'd suggest you use a classList polyfill or make addClass its own function that takes the element as an argument instead. Commented Jul 9, 2016 at 0:07
  • 3
    this.classList.add("class") Commented Jul 9, 2016 at 0:17
  • Why would adding a useful function as method make the code confusing @RyanO'Hara? Commented Jul 9, 2016 at 0:19
  • 2
    @AngelPolitis A future version of DOM could define a native addClass which behaves differently than yours. It could even have a setter, and when you attempt to define your method, something unexpected may happen. Never alter an object you don't own. Commented Jul 9, 2016 at 0:22

2 Answers 2

4

I believe you would add it to the Element object prototype

Element.prototype.addClass = function(className){
    // something
}
Sign up to request clarification or add additional context in comments.

5 Comments

Absolutely correct @realseanp. I just tested and it works! Thanks a lot.
Avoid adding to prototypes that you don't own.
OP's question was "which object's prototype should I add this method to". Element is the object in which the method would be added to to accomplish what the question is asking. Down voting based on opinion is unwarranted.
It's not an opinion @realseanp... it's a known thing programmers.stackexchange.com/questions/104320/…. Just cause you answer what the OP asked, doesn't mean it's not a bad practice.
Most people consider this bad practice. I consider this bad practice. But I agree with realseanp that the down-votes are unwarranted because this does answer the question that was asked so the answer is both literally correct and helpful to the OP. Having said that, it would be a better answer if it included even a one-sentence explanation about why this is bad practice, and it could perhaps give an example of testing if the method already exists on the prototype rather than blindly overwriting it.
2

Use Element.classList.add().

Natively, it's available in all modern browsers including IE10 and up.

The link above also provides a polyfill for all non-supported browsers.

4 Comments

This should be the accepted answer. Coupled with a polyfill this is the most past/future-friendly option.
But is the question really about adding classes, or is it "How do I add methods to DOM elements?" with classes just being an example? (I know that extending native prototypes is widely considered to be a bad practice - and I agree that it is - but that doesn't mean there isn't a clear answer on how to do it.)
@Seth no it shouldn't. My question was clear. I wasn't asking for another way to add a class, but in which object's prototype I should add a method that does that. This a useful suggestion that doesn't, however, answer the question.
Understood. If you're trying to learn how to work with prototypes by creating a library similar to jQuery through native objects, then I 100% agree with you. However, if you want to write future-proof JavaScript for a production application, document.getElementById("example").onclick = function() { this.classList.add("class"); } is a well tested alternative!

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.