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

201
Vistas
How to test svelte input attribute agains multiple options

i have store value and two sets of data:

$selectedColor

const productColor = {
   options: [
      {id:0, title: 'white'},
      {id:1, title: 'black'},
      {id:2, title: 'red'},
      {id:3, title: 'yellow'}
   ],
}


const productType = {
   options: [
      {id:0, title: 'somethings', hiddenInColor: 'white'},
      {id:1, title: 'somethings', hiddenInColor: 'black'},
      {
         id:2, 
         title: 'somethings', 
         hiddenInColor: 
        //here should be black AND white
        //problem is I can't understand how to use nested json with disable attribute (see below) 
      }
   ],
}

then i have populate inputs (type: radio) for product type and color

{#each productType.options as option (option.id)}
            {#if activeTab !== option.hiddenIn}
                <li>
                    <label>
                        <input type="radio" name="name" 
                            value={option.id} 
                            on:change={() => optionHasChanged(option.name, somethings)}
                            disabled={option.hiddenInColor == $selectedColor}
                            bind:group={defaultProductType}>
                        {option.title}
                    </label>
                </li>
            {/if}
        {/each}

I was trying to -> disable input IF $selectedColor == hiddenInColor

i did it like this ->

disabled={option.hiddenInColor == $selectedColor}

and it works well. But... only when you testing it against 1 value

i cant understand how to check more then 1 color at once. So basically, using my const productType -> i would like to disable id:3 if $selectedColor == 'black' || 'white'

any advices? Or maybe there is more compact solution to this? Thanks

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

0

The simplest way to achieve this would be using an array hiddenInColors instead of a single scalar value:

const productType = {
   options: [
      {id:0, title: 'somethings', hiddenInColors: ['white']},
      {id:1, title: 'somethings', hiddenInColors: ['black']},
      {id:2, title: 'somethings', hiddenInColors: ['black','white']},
   ],
}

and then set the disabled option like this:

disabled={option.hiddenInColors.includes($selectedColor)}
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