Skip Navigation

[Resolved] Conditional fields and conditional css

This thread is resolved. Here is a description of the problem and solution.

Problem:
The user wanted to style a div based on the value of a custom field

Solution:
User code has some errors. Check the solution here https://toolset.com/forums/topic/conditional-fields-and-conditional-css/#post-1582341

Relevant Documentation:

- https://toolset.com/documentation/legacy-features/views-plugin/conditional-html-output-in-views/

- https://www.w3schools.com/cssref/sel_class.asp

This support ticket is created 4 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

This topic contains 2 replies, has 2 voices.

Last updated by samuelH 4 years, 8 months ago.

Assisted by: Jamal.

Author
Posts
#1582289
Skjermbilde 2020-04-11 kl. 15.54.10.png

Hi,
I have a conditional field, that I would like to change the backround color of depending on the conditional value.

I have put in this code:

<div class="q-gray1">
Status: [types field='statusbeskrivelse'][/types]

<div class="status">[wpv-conditional if="( $(wpcf-statusbeskrivelse) eq '2' )"]status-red[/wpv-conditional] [wpv-conditional if="( $(wpcf-statusbeskrivelse) eq '1' )"]status-gronn[/wpv-conditional]</div></div>

And this CSS
status-red {background-color: red;}
status-gronn {background-color: green;}

The conditional field shows the text value, but are not changing the background color. Where am I doing wrong here?

The field should show the value of the condition (This works), but not the words "status-gronn" The background if the field should turn green.

#1582341

Hello and thank you for contacting the Toolset support.

I think that both your view and CSS codes are not correct. The CSS code should use (.) before the class name. Then the name should be used inside the class attribute of your div tag. Try the following:
1. CSS

.status-red {background-color: red;}
.status-gronn {background-color: green;}

2. View:

<div class="q-gray1">
Status: [types field='statusbeskrivelse'][/types]

<div class="status [wpv-conditional if="( $(wpcf-statusbeskrivelse) eq '2' )"]status-red[/wpv-conditional] [wpv-conditional if="( $(wpcf-statusbeskrivelse) eq '1' )"]status-gronn[/wpv-conditional]">[wpv-conditional if="( $(wpcf-statusbeskrivelse) eq '2' )"]status-red[/wpv-conditional] [wpv-conditional if="( $(wpcf-statusbeskrivelse) eq '1' )"]status-gronn[/wpv-conditional]</div></div>

Read more about the class CSS selector here hidden link

#1582787

My issue is resolved now. Thank you!