Search code examples
moovwebtritium

In Tritium, what's the difference between add_class and attribute


I can use add_class("classname") to add a class attribute to one of my elements, but I can also use attribute("class", "classname") to do the same.

What's the difference between the two functions? Any gotchas?


Solution

  • Yup, the tritium function add_class(...) will append the given argument to the class attribute in the node you're currently in (also prepending a space to separate it from other class names).

    On the other hand, calling attribute("class", "classname") will actually clobber whatever class names already existed with the value you provided.

    Below is an example illustrating both in tritium tester:

    http://tritium.moovweb.com/43ecf5fdbc4bf6b07312372724df5a2522474cc3