Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

226
Vistas
Add style according to the value of x-text/html value

I want to center my td element when the text is equals to "-". If the text is equals to something else, I want it align at the left.

How can I do that?

<td x-bind:style="$el.textContent == '-' ? { 'text-align': 'left' } : { 'text-align': 'center' }" x-text="format(variable)"></td>

Yes, I could simple replace the $el.textContent by format(variable), but I would like to not call format(variable) twice.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can use the variable itself at the style binding:

<td x-bind:style="variable !== '' ? { 'text-align': 'left' } : { 'text-align': 'center' }" x-text="format(variable)"></td>

This way the style binding is reactive as well.

Edit:

If you really want to avoid using variable at the style binding, you can combine x-init with the $nextTick magic to wait until Alpine.js finishes updating the DOM, so you can grab the actual content of the cell and update the cell's style if it's just a dash. However this method is not reactive.

<td x-init="$nextTick(() => {if ($el.textContent == '-') {$el.style['text-align'] = 'center'}})"
    x-text="format(variable)">
</td>
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda