/ Published in: JavaScript
Public domain. find_index is required because not all versions of IE support Array.indexOf.
Expand |
Embed | Plain Text
function get(elmnt) { return document.getElementById(elmnt); } function find_index(array, string) { var i = 0; for(i=0;i<array.length;i++) { if(array[i]==string) break; } return array[i] == string ? i : -1; } function add_class(elmnt, new_class) { classes = get(elmnt).className.split(' '); if(find_index(classes, new_class)==-1) { // new_class is not present classes[classes.length] = new_class; get(elmnt).className = classes.join(' '); } } function remove_class(elmnt, class_to_remove) { classes = get(elmnt).className.split(' '); class_index = find_index(classes, class_to_remove); if(class_index!=-1) { // class_to_remove is present classes.splice(class_index, 1); get(elmnt).className = classes.join(' '); } }
You need to login to post a comment.
