Newer Version Available

This content describes an older version of this product. View Latest

Working with Attribute Values in JavaScript

These are useful and common patterns for working with attribute values in JavaScript.

Example

In these examples, cmp is a reference to a component in your JavaScript code. It’s usually easy to get a reference to a component in JavaScript code.

Get an Attribute Value

To get the value of a component’s label attribute:

1var label = cmp.get("v.label");

Set an Attribute Value

To set the value of a component’s label attribute:

1cmp.set("v.label","This is a label");

Get a Boolean Attribute Value

To get the boolean value of a component’s myString attribute:

1var myString = $A.util.getBooleanValue(cmp.get("v.myString"));
For example, the following attribute returns true when passed into $A.util.getBooleanValue().
1<aura:attribute name="myString" type="String" default="my string"/>
If the attribute is of type Boolean, cmp.get("v.myBoolean") returns the boolean value and $A.util.getBooleanValue() is not needed.

Validate that an Attribute Value is Defined

To determine if a component’s label attribute is defined:

1var isDefined = !$A.util.isUndefined(cmp.get("v.label"));

Validate that an Attribute Value is Empty

To determine if a component’s label attribute is empty:

1var isEmpty = $A.util.isEmpty(cmp.get("v.label"));