Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 5th, 2006, 03:25 PM
spolsky
Guest
 
Posts: n/a
Default apply multiple classes

it is possible by dom(scripting) to apply multiple classes to the
className attribute.
elmRef.className = 'Class1 Class2 ...' ;

but what if some other script assigns its own classes. in this case, it
would be possible to track our classes in the className attribute(by
searching in the string) and remove or add them accordingly but aren't
there any flexible approach? it would be better if className is a
collection that we could determine easily if required classes are
applied or not.
or is there any different/better approach to apply our multiple classes
without depending on other scripts on the page?

thanks

  #2  
Old August 7th, 2006, 01:55 AM
Den Odell
Guest
 
Posts: n/a
Default Re: apply multiple classes

Only thing I can think of is to write your own JS functions for
handling reading back and adding to className, splitting an array on
the 'space' character should start you off. I can imagine someone must
have written one already.. anyone?

Good luck
Den


spolsky wrote:
Quote:
it is possible by dom(scripting) to apply multiple classes to the
className attribute.
elmRef.className = 'Class1 Class2 ...' ;
>
but what if some other script assigns its own classes. in this case, it
would be possible to track our classes in the className attribute(by
searching in the string) and remove or add them accordingly but aren't
there any flexible approach? it would be better if className is a
collection that we could determine easily if required classes are
applied or not.
or is there any different/better approach to apply our multiple classes
without depending on other scripts on the page?
>
thanks
  #3  
Old August 7th, 2006, 01:45 PM
Harlan Messinger
Guest
 
Posts: n/a
Default Re: apply multiple classes

spolsky wrote:
Quote:
it is possible by dom(scripting) to apply multiple classes to the
className attribute.
elmRef.className = 'Class1 Class2 ...' ;
>
but what if some other script assigns its own classes. in this case, it
would be possible to track our classes in the className attribute(by
searching in the string) and remove or add them accordingly but aren't
there any flexible approach? it would be better if className is a
collection that we could determine easily if required classes are
applied or not.
Untested code:

function hasClassName(str, className)
{
var strPadded = " " + str + " ";
var classNamePadded = " " + className + " ";
return strPadded.indexOf(classNamePadded) != -1;
}

function removeClassName(element, className)
{
var strPadded = " " + element.className + " ";
var classNamePadded = " " + className + " ";
var result = strPadded.replace(classNamePadded, "");
result = result.substring(1, result.length - 1);
element.className = result;
}

function addClassName(element, className)
{
var str = element.className;
if (!hasClassName(str, className))
{
str += " ";
str += className;
element.className = str;
}
}
?
Quote:
or is there any different/better approach to apply our multiple classes
without depending on other scripts on the page?
>
thanks
>
 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles