Created
April 25, 2014 17:50
-
-
Save nachodd/11297760 to your computer and use it in GitHub Desktop.
jQuery plugin para reemplazar una clase css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Closure para el evitar conflicto de variables, se le pasa el obj jQuery | |
(function ( $ ) { | |
/** | |
* Elimina la clase que empieza con "startsWith" y añade la clase newClass (o no hace nada, si es vacia) | |
* @param {[string]} startsWith string con el que debe comenzar la clase a eliminar | |
* @param {[string]} newClass string con la nueva clase, o string vacio | |
* @return {[jQobj]} Objeto jQuery | |
*/ | |
$.fn.replaceClass = function( startsWith, newClass ) { | |
// A cada uno de los elementos de la selecion ( this ), le aplica la funcion.. | |
return this.each(function(i, el){ | |
var $el = $(el); | |
var classes = $el.attr("class").split(" ").filter(function(c) { | |
return c.lastIndexOf(startsWith, 0) !== 0; | |
}); | |
$el.attr("class", classes.join(" ")).addClass( newClass ); | |
}); | |
}; | |
}( jQuery )); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment