§ Simpler adding & removing element classnames
Just use the javascript replace function...
The way I used to do this is just dumb; the replace function is quite straightforward and most likely faster.
function removeClassName(el, name) {
el.className = el.className.replace(name, '');
}
function addClassName(el, name) {
//don't waste time if it's already there
if(el.className.indexOf(name)>-1)
return;
el.className += ' '+name;
}
//or, if you have reason to believe it's not already there, just
myelement.className+=' myclassname';
//swap known names
myelement.className = myelement.className.replace('oldName', 'newName');
last edited on May 7th, 2010 at 6:35 PM
Categories
Comments
- backlinks on "Adjust iframe height to contents" - Definitely helped me.
- Slashback on "Simpler validate form fields are filled in (no additional class names)" - You are welcome!
- humour on "Simpler validate form fields are filled in (no additional class names)" - thanks for the script !
- Slashback on "Simpler validate form fields are filled in (no additional class names)" - Thank you. I'm glad it helped!
- buy site on "Javascript function to add css" - Well, i have faced this problem a lot of times and now i got how to add CSS to my java script. I bookmarked your page and will come…