Frontendplace Blog

Sharing Frontend developing ideas

Prototype.js what to know about hasClassName and addClassName

Posted by info on February 11th, 2011

Prototype.js v1.7 and before replaces and detects classnames using a regular expression without escaping special characters like “+”. So when you check or add a classname with a + character in the string you get a different result than you’ll expect. For example we use the classname for adding validation rules. Checking startDateElement.hasClassName(“before-today+1y”) will always return false, even if the string token is part of the classname. The addClassName function uses the hasClassName function before it adds the extra string. So this will result in always adding the string even if it is already there and you end up having two strings the same inside the total classname string.