Friday, 16 August 2013

querySelectorAll and Element.prototype

querySelectorAll and Element.prototype

I'm using this custom library
function $$(selector, el) {
if (!el) {
el = document;
}
return el.querySelectorAll(selector);
}
Element.prototype.addClass = function(className) {
if (!this.hasClass(className))
this.className += ' ' + className;
};
but when i'm trying to use function from element prototype on each element
from selector $$, i get error
var $submenu = $$('.nav-submenu li', this);
for (var i in $submenu) {
$submenu[i].addClass('active');
}
Uncaught TypeError: Object 14 has no method 'addClass'
why elements in $submenu array have no Element.prototype methods?

No comments:

Post a Comment