Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

130
Visualizações
Svelte change color of buttons indivually

I have some buttons and when i click on each of them the background-color should change. Right now it changes the color of all buttons when one is clicked. However it should be for each button individually. Does someone know how to do that in?

That's my main svelte class:

<script lang="ts">
  import Button from './Button.svelte';
  let empty = false;

  function changeBtnState() {
    if (empty) {
      empty = false;
    } else {
      empty = true;
    }
  }
</script>

<Button label="process" {empty} onClickFn={changeBtnState} />
<Button label="date" {empty} onClickFn={changeBtnState} />

<style lang="scss">
    .main-button.empty {
        background-color: red;
    }
</style>

And that's my button component

<script lang="ts">
    export let label: string = 'test';
    export let onClickFn: () => void = () => {
      return;
    };
    export let empty = false;
</script>

<button
  type="submit"
  class="main-button"
  class:empty={empty}
  on:click={onClickFn}>{label}</button
>

<style lang="scss">
    .main-button{
        background-color: #f2eee2;
    }
</style>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The problem is that you bind both your button's empty attribute to the same variable:

<Button label="process" {empty} onClickFn={changeBtnState} />
<Button label="date" {empty} onClickFn={changeBtnState} />

You need a separate one for each button.

about 4 years ago · Juan Pablo Isaza Relatório

0

What you can do it's to integrate the changeBtnState inside the Button component. So the empty variable is defined IN the component and is different for each of them. Another option if you don't want to integrate it in one component, is yo create an array of "empty" variables (1 per button).

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda