Set Attribute
Dynamic custom attribute setting.
Last updated
Was this helpful?
Dynamic custom attribute setting.
Last updated
Was this helpful?
The attr(expression, attribute)
allows you to dynamically set any custom attribute based on a specified condition or variable value. This makes it easy to update HTML elements with conditional attributes that respond to form interactions, scores, and other data-driven changes.
Attribute: sf-react = attr(expression, attribute)
Where: Place on any element within your form to dynamically set an attribute’s value based on an expression.
Usage: Use attr()
to add or modify any custom attribute using the expression
to define the attribute
value. When the expression
evaluates to true, the attribute is set accordingly.
This example demonstrates how to dynamically set a custom data-status
attribute based on a scoring variable.
In this example:
Condition: The attribute data-status
updates based on the score in $v.score
.
Result: When $v.score
is greater than 10, the data-status
attribute will show "achieved"
; otherwise, it will display "pending"
.
Pro Tips
Multiple Dynamic Attributes: You can apply multiple attr()
reactivity attributes on different (or same!) elements within the same form.
Complex Conditions: Use inline JavaScript or combined expressions for complex logic within attr()
.
This feature provides full control over custom attributes based on real-time data, ideal for implementing conditional visuals, accessibility enhancements, or toggling attributes to improve user engagement.