ClassArray is a collection of classes applied to an element. As a class array is modified using the Add, Remove and Clear functions, the class attribute of the element are updated accordingly. Checking of classes is more reliable using the Classes property than checking ClassName propety because of classes that might make partial matches.


   1:  <div id="DivTest" class="floatwidget green">
   2:  </div>
   4:  <script type="text/javascript">
   5:      Page.OnLoad.Add(function() {
   6:          if($Div("DivTest").ClassName().Contains("widget")){    // results to true even though it's wrong
   7:              otherFunction($Div("DivTest"));
   8:          }
  10:          if($Div("DivTest").Classes.Contains("widget")){    // results to false, there is no "widget" class applied
  11:              otherFuncion2($Div("DivTest"));
  12:          }
  13:      });
  14:  </script>

You can use the class array as well to easily apply and unapply classes without using string modification.

   1:  $Div("id").Classes.Add("selected");    // Adds 'selected' to the class attribute
   2:  $Div("id2").Classes.Remove("selected");    // Removes 'selected' from the class attribute

All normal Array functions still apply. Modifying the array with normal JavaScript functions will not affect the class attribute of the parent element.






Owner : Element – The element to which the ClassArray belongs to.









Add(String class) : undefined – Adds and applies the given class to the element.

Clear() : undefined – Removes all classes applied to the element.

Remove(String class) : undefined – Remove the specified class and unapplies it from the element.

Set(String class) : undefined – Removes all classes and applies the specified class as the only class.

ToString() : String – Returns the full class attribute value of the element.

Last edited Jan 5, 2011 at 3:01 AM by dahrkdaiz, version 2


No comments yet.