0

Is there a way for me to add dynamic styling depending what the result in my div will be?

I have created a function which will calculate a total number and then change the data property to return either 'Low' or 'Good' depending on what the count will be.

I have two different classes which I have created:

.good-result {
  color: green;
  font-weight: bold;
}
.poor-result {
  color: orange;
  font-weight: bold;
}

My dynamic styling looks like this

<div class="resultValue">Water intake: <span :class="'Low'?'poor-result' : 'good-result'">{{ resultsWater }}</span></div>

If the data property resultsWater says 'Low' I want to try and apply the .poor-result style else the .good-result property, currently it only displays the .poor-result no matter if the divs value is 'Low' or 'Good'

1
  • 2
    All non-empty strings are truthy, so 'Low' will always evaluate to true. Commented Dec 24, 2020 at 11:32

1 Answer 1

1

Try to use string interpolation and compare resultsWater to 'Low':

 :class="`${resultsWater === 'Low'?'poor-result' : 'good-result'}">
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.