Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine x-show & x-text toggle in alpine.js

I am new to alpine.js and i need to toggle the innerText of an element, does anyone know ho can i combine showing an element and changing the innerText of another element? here is my code

<div x-data="{ numOrder: false, delNumOrder: 'Remove your order number' }">
  <button x-on:click="numOrder = !numOrder">
  Insert your order number</button>
  <input x-show="numOrder" type="text" placeholder="Order Number">
</div>

I need to overide the button text by delNumOrder, i have tried using x-text :

<button x-on:click="numOrder = !numOrder" x-text="delNumOrder = !delNumOrder">

Can i use something like ?

<div x-data="{ numOrder: false, delNumOrder = delNumOrder ? 'Insert your order number' : 'Remove your order number' }"></div>
like image 215
Elchy Avatar asked Mar 02 '23 01:03

Elchy


1 Answers

You can use an expression inside of x-text, in this case the ternary for the text: numOrder ? 'Remove your order number': 'Insert your order number'. You probably want to do something like the following:

<div x-data="{ numOrder: false }">
  <button
    x-on:click="numOrder = !numOrder"
    x-text="numOrder ? 'Remove your order number': 'Insert your order number'"
  ></button>
  <input x-show="numOrder" type="text" placeholder="Order Number">
</div>
like image 110
Hugo Avatar answered May 11 '23 02:05

Hugo