x-bind ====== `x-bind` allows you to set HTML attributes on elements based on the result of JavaScript expressions. For example, here's a component where we will use `x-bind` to set the placeholder value of an input.
[Shorthand syntax](#shorthand-syntax) ------------------------------------- If `x-bind:` is too verbose for your liking, you can use the shorthand: `:`. For example, here is the same input element as above, but refactored to use the shorthand syntax. > Despite not being included in the above snippet, `x-bind` cannot be used if no parent element has `x-data` defined. [→ Read more about `x-data`](/directives/data) [Binding classes](#binding-classes) ----------------------------------- `x-bind` is most often useful for setting specific classes on an element based on your Alpine state. Here's a simple example of a simple dropdown toggle, but instead of using `x-show`, we'll use a "hidden" class to toggle an element.
 
Dropdown Contents...
Dropdown Contents...
Now, when `open` is `false`, the "hidden" class will be added to the dropdown. ### [Shorthand conditionals](#shorthand-conditionals) In cases like these, if you prefer a less verbose syntax you can use JavaScript's short-circuit evaluation instead of standard conditionals:
The inverse is also available to you. Suppose instead of `open`, we use a variable with the opposite value: `closed`.
### [Class object syntax](#class-object-syntax) Alpine offers an additional syntax for toggling classes if you prefer. By passing a JavaScript object where the classes are the keys and booleans are the values, Alpine will know which classes to apply and which to remove. For example:
This technique offers a unique advantage to other methods. When using object-syntax, Alpine will NOT preserve original classes applied to an element's `class` attribute. For example, if you wanted to apply the "hidden" class to an element before Alpine loads, AND use Alpine to toggle its existence you can only achieve that behavior using object-syntax: