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

148
Visualizações
How to toggle div colors in one line based on 3 values

I have values coming back from an API (easy,medium,hard) : For each of these values, i'd like to affect a color depending on the difficulty and I'd like to do it in one line. So basically here is what I like to do in some sort of an HTML algorithm ( I'm using VueJS):

   <div if(difficulty is easy) then color is green 
    else if (difficulty is medium) then color is yellow
   else if (difficulty is hard) then color is red></div>

Is it even possible to do this in one line of code with a v-if or a ternary operator?? and If so, how can I do it?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Maybe like following snippet :

new Vue({
  el: '#demo',
  data() {
    return {
      difficulty: 'medium',
    }
  },
})
.red {
  color: red;
}
.green {
  color: green;
}
.yellow {
  color: yellow;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">
  <div :class="difficulty === 'ease' ? 'green' : difficulty === 'medium' ? 'yellow' : 'red'">medium</div>
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

I just would use some class binding with :class. You can use the difficulty as a string itself and bind the class to the dom object.

Vue.createApp({
  data() {
    return {
      difficulty: 'easy'
    }
  },
  methods: {
    changeDif() {
      // just for demo purposes
      const difficulties = ['easy', 'medium', 'hard']
      let i = difficulties.indexOf(this.difficulty) + 1
      if (i === difficulties.length)i = 0
      this.difficulty = difficulties[i]
    }
  }
}).mount('#demo')
.box {
  width: 24px;
  height: 24px;
  margin: 4px;
}
.easy {
  background-color: green;
}
.medium {
  background-color: orange;
}
.hard {
  background-color: darkred;
}
<script src="https://unpkg.com/vue@next"></script>

<div id="demo" class="demo">
  <button @click="changeDif()">{{ difficulty }}</button>
  
  <div :class="difficulty" class="box">
  </div>
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

I'd use CSS.

<style>
.easy {background-color: green;}
.medium {background-color: yellow;}
.hard {background-color: red;}
</style>
<div class='easy'>easy</div>
<div class='medium'>medium</div>
<div class='hard'>hard</div>
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