I would like to replicate jQuery’s addClass function using plain javascript
So far, I have made this function:
function addClass(el,cl){
el.className+=(el.className?' ':'')+cl
}
It works well, but It uses this syntax:
addClass(element,class)
I want it to use this syntax:
element.addClass(class)
How can I do that?
thanks 🙂
You can make it a prototype to extend the
Elementclass.example
As minitech pointed out though, this won’t work reliably in all browsers (namely IE).