Dynamicly add classes to selectoptions

By: Sebastian Schmidgen | Asked: 09/18/2023
ForumsCategory: Code HelpDynamicly add classes to selectoptions
Sebastian Schmidgen asked 10 months ago
Hi, we populate an optionsfield with dynamic values like in this article: https://formidableforms.com/knowledgebase/frm_setup_new_fields_vars/#kb-populate-a-field-with-wordpress-posts That works. Now we would like to add dynamic classes to each option. We tried the following, which doesn't work: $values['class'][$p->ID] = 'myClass';   Is there a way to add a specific class to each select value?   Thank you in advance Sebastian
2 Answers
Best Answer
Victor Font answered 10 months ago
You can add classes to $values['classes']. The classes are applied to the field container. However, please keep in mind that the level of customization may vary across different browsers. Some properties may not be supported or may have limited effects on select options. It's always a good practice to test your styles across different browsers to ensure consistent behavior.
Sebastian Schmidgen answered 10 months ago
Hi Victor, thank you for your help. I will try it with 'classes' instead of 'class'. Good to know. I found another solution with jQuery to add specific classes that worked. I look for the content of the selectoption and add the class this way.     <script>     (function($){         $("select option:contains('Natur')").addClass("natur");         $("select option:contains('Mensch')").addClass("mensch");         $("select option:contains('Sprache')").addClass("sprache");         $("select option:contains('Gesundheit')").addClass("gesundheit");         $("select option:contains('Gestaltung')").addClass("gestaltung");     })(jQuery);     </script>  

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right