Add 'hasClass' function

Also fix issue with the mapOne function
This commit is contained in:
Brian Buller 2015-11-02 14:26:50 -06:00
parent e1123273c4
commit 04e000fa8d
1 changed files with 8 additions and 1 deletions

9
B.js
View File

@ -29,7 +29,7 @@ function B(els, attrs) {
// Map a function to the first element in 'this'
B.prototype.mapOne = function(callback) {
var m = this.map(callback);
return m.length > 1 ? m : m[0];
return m.length > 1 ? m[0] : m;
};
// Update css for each element in 'this'
@ -57,6 +57,13 @@ function B(els, attrs) {
}
};
// Check if the first element has the requested class
B.prototype.hasClass = function(cls) {
return this.mapOne(function(el){
return (' ' + el.className + ' ').indexOf(' ' + cls + ' ') > -1;
});
}
// Add a class to each element in 'this'
B.prototype.addClass = function(classes) {
var className = "";