blob: 0707fa305de7968be0af1930de23fa6aecd9bdf0 [file] [log] [blame]
To modify tag attributes in a component's HTML markup we can use class _org.apache.wicket.AttributeModifier_. This class extends _org.apache.wicket.behavior.Behavior_ and can be added to any component via the _Component_'s _add_ method. Class _Behavior_ is used to expand component functionalities and it can also modify component markup. We will see this class in detail later in <<advanced.adoc#_enriching_components_with_behaviors,chapter 19.1>>.
As first example of attribute manipulation let's consider a _Label_ component bound to the following markup:
[source,html]
----
<span wicket:id="simpleLabel"></span>
----
Suppose we want to add some style to label content making it red and bolded. We can add to the label an _AttributeModifier_ which creates the tag attribute _style_ with value _color:red;font-weight:bold_:
[source,java]
----
label.add(new AttributeModifier("style", "color:red;font-weight:bold"));
----
If attribute _style_ already exists in the original markup, it will be replaced with the value specified by _AttributeModifier_. If we don't want to overwrite the existing value of an attribute we can use subclass _AttributeAppender_ which will append its value to the existing one:
[source,java]
----
label.add(new AttributeAppender("style", "color:red;font-weight:bold"));
----
We can also create attribute modifiers using factory methods provided by class _AttributeModifier_ and it's also possible to prepend a given value to an existing attribute:
[source,java]
----
//replaces existing value with the given one
label.add(AttributeModifier.replace("style", "color:red;font-weight:bold"));
//appends the given value to the existing one
label.add(AttributeModifier.append("style", "color:red;font-weight:bold"));
//prepends the given value to the existing one
label.add(AttributeModifier.prepend("style", "color:red;font-weight:bold"));
----