Title: Display conditional text in calculation
Last modified: March 18, 2019

---

# Display conditional text in calculation

 *  Resolved [hskokanek](https://wordpress.org/support/users/hskokanek/)
 * (@hskokanek)
 * [7 years, 3 months ago](https://wordpress.org/support/topic/display-conditional-text-in-calculation/)
 * Hi,
 * is there a way to conditionally display a text based on the option selected for
   the calculation? Ie. depending on what I select from the drop down menu, there
   will be a specific text displayed after the result (or in a separate text field).
 * Thank you
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fdisplay-conditional-text-in-calculation%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 10 replies - 1 through 10 (of 10 total)

 *  Thread Starter [hskokanek](https://wordpress.org/support/users/hskokanek/)
 * (@hskokanek)
 * [7 years, 3 months ago](https://wordpress.org/support/topic/display-conditional-text-in-calculation/#post-11326366)
 * also – can the “number input” field show units? (like “kg”)?
 * Thx again!
 *  Plugin Author [codepeople](https://wordpress.org/support/users/codepeople/)
 * (@codepeople)
 * [7 years, 3 months ago](https://wordpress.org/support/topic/display-conditional-text-in-calculation/#post-11326396)
 * Hello [@hskokanek](https://wordpress.org/support/users/hskokanek/)
 * Concerning to your first question. Assuming the DropDown field is the fieldname1,
   and the possible values are: 1,2, and 3 and the texts to display: First text,
   Second text and Third text, respectively. The equation to implement could be:
 *     ```
       (function(){
         switch(fieldname1){
            case 1: return 'First text';
            case 2: return 'Second text';
            case 3: return 'Third text';
         }
       })()
       ```
   
 * Of course, there are many other possible implementations.
 * Concerning to your second question. The Kg text into a number field makes it 
   invalid, however, you can emulate it with a currency field, entering the text
   Kg as the currency symbol in the field’s settings.
 * Best regards.
 *  Thread Starter [hskokanek](https://wordpress.org/support/users/hskokanek/)
 * (@hskokanek)
 * [7 years, 3 months ago](https://wordpress.org/support/topic/display-conditional-text-in-calculation/#post-11326532)
 * Wow, thanks for the fast heads up!
 * Actually I have tried the Currency field, but for some reason it is not displaying
   the currency symbol/description in the form on the live site (it is showing in
   the back-end, though)
 * In the suggested equation – where in the following do I put the actual equation
   like PREC(fieldname2*fieldname3) to return the calculation? Apologies, if this
   is out of the scope of this forum. I couldnt get the suggested code to work. 
   Thx
 * (function(){
    switch(fieldname3){ case Ibuprofen: return ‘First text’; case Nurofen:
   return ‘Second text’; case Panadol: return ‘Third text’; case Paralen: return‘
   Fourth text’; } })()
 *  Plugin Author [codepeople](https://wordpress.org/support/users/codepeople/)
 * (@codepeople)
 * [7 years, 3 months ago](https://wordpress.org/support/topic/display-conditional-text-in-calculation/#post-11326640)
 * Hello [@hskokanek](https://wordpress.org/support/users/hskokanek/)
 * Your are merging different things here. Do you want to display a text, the result
   of the mathematical operation, or concatenate both?
 * By the way there are different issues in your code. First, the PREC operation
   requires two attributes: PREC(X,Y) returns the number X rounded with Y decimal
   places. If you want rounding the result to the nearest integer, the recommended
   operation would be: ROUND
 * Second, in javascript the text values should be wrapped between single or double
   quotes. For example, merging both equations, the solution would be:
 *     ```
       (function(){
           var result = ROUND(fieldname2*fieldname3);
           switch(fieldname3){
               case 'Ibuprofen': return 'First text: '+result;
               case 'Nurofen': return 'Second text: '+result;
               case 'Panadol': return 'Third text: '+result;
               case 'Paralen': return 'Fourth text: '+result;
           }
       })()
       ```
   
 * Best regards.
 *  Thread Starter [hskokanek](https://wordpress.org/support/users/hskokanek/)
 * (@hskokanek)
 * [7 years, 3 months ago](https://wordpress.org/support/topic/display-conditional-text-in-calculation/#post-11326675)
 * Thx, this would be my poor explanation 🙁 Apologies for that. That is probably
   why the function is not returning anything.
 * The drop down front-end options (fieldname3) are ‘text values’, however, for 
   the back-end calculation I am using a ‘numerical value’ assigned to each of the
   text options and then displaying the calculation result lets say in (fieldname1)
   Calculated field.
 * Maybe it is actually better to display that using the Summary field (where fieldname1
   is the Calculated field)
 *     ```
       (function(){
           fieldname1;
           switch(fieldname3){
               case 'Ibuprofen': return 'First text';
               case 'Nurofen': return 'Second text';
               case 'Panadol': return 'Third text';
               case 'Paralen': return 'Fourth text';
           }
       })()
       ```
   
    -  This reply was modified 7 years, 3 months ago by [hskokanek](https://wordpress.org/support/users/hskokanek/).
    -  This reply was modified 7 years, 3 months ago by [hskokanek](https://wordpress.org/support/users/hskokanek/).
    -  This reply was modified 7 years, 3 months ago by [hskokanek](https://wordpress.org/support/users/hskokanek/).
 *  Plugin Author [codepeople](https://wordpress.org/support/users/codepeople/)
 * (@codepeople)
 * [7 years, 3 months ago](https://wordpress.org/support/topic/display-conditional-text-in-calculation/#post-11326734)
 * Hello [@hskokanek](https://wordpress.org/support/users/hskokanek/)
 * In the equations are used always the values of fields and not their texts. So,
   you cannot compare directly with the texts: Ibuprofen, Nurofen, Panadol or Paralen
 * Furthermore, in your case there is a possible conflict because there are choices
   in the field with the same value (the values of the first and second choices 
   are 10, and for the third and fourth choices 15)
 * What would be the solution?
 * There are different alternatives. For example, implements your equation as follows:
 *     ```
       (function(){
           var result = ROUND(fieldname2*fieldname3), 
               text = jQuery('[id="'+getField(3).name+'"] option:selected').text();
   
           switch(text){
               case 'Ibuprofen': return 'First text: '+result;
               case 'Nurofen': return 'Second text: '+result;
               case 'Panadol': return 'Third text: '+result;
               case 'Paralen': return 'Fourth text: '+result;
           }
       })()
       ```
   
 * Best regards.
 *  Thread Starter [hskokanek](https://wordpress.org/support/users/hskokanek/)
 * (@hskokanek)
 * [7 years, 3 months ago](https://wordpress.org/support/topic/display-conditional-text-in-calculation/#post-11326854)
 * OK this is really cool!. In order to be able to style the values separately, 
   I have split this using two Summary fields and the code:
 *     ```
       (function(){
           var result = ROUND(fieldname2*fieldname3), 
               text = jQuery('[id="'+getField(3).name+'"] option:selected').text();
   
           switch(text){
               case 'Ibuprofen': return 'maximálně však 3 dávky denně po 8mi hodinách';
               case 'Nurofen': return 'maximálně však 3 dávky denně po 8mi hodinách';
               case 'Panadol': return 'maximálně však 4 dávky denně po 6ti hodinách';
               case 'Paralen': return 'maximálně však 4 dávky denně po 6ti hodinách';
           }
       })()
       ```
   
 * Though the ‘var result’ function is now unnecessary probably (but code will not
   work without it).
 * Is it possible to build the fields to be next to each other (ie. as in two columns)
   in the form builder?
 * BTW – if the custom CSS through the Customize Form Design is being used, it will
   not be applied if the form is placed into a PopUp.
 * Thanks!! This is unheard of level of support!!
 *  Plugin Author [codepeople](https://wordpress.org/support/users/codepeople/)
 * (@codepeople)
 * [7 years, 3 months ago](https://wordpress.org/support/topic/display-conditional-text-in-calculation/#post-11326892)
 * Hello [@hskokanek](https://wordpress.org/support/users/hskokanek/)
 * Yes, the code is unnecessary, but you need to tell the equation that depends 
   on the fieldame3, to evaluate it every time the fieldname3 field varies its value.
   So, you can edit the equation as follows:
 *     ```
       (function(){
           var tmp = fieldname3, 
               text = jQuery('[id="'+getField(3).name+'"] option:selected').text();
   
           switch(text){
               case 'Ibuprofen': return 'maximálně však 3 dávky denně po 8mi hodinách';
               case 'Nurofen': return 'maximálně však 3 dávky denně po 8mi hodinách';
               case 'Panadol': return 'maximálně však 4 dávky denně po 6ti hodinách';
               case 'Paralen': return 'maximálně však 4 dávky denně po 6ti hodinách';
           }
       })()
       ```
   
 * If you want to distribute the form’s fields in columns, please, read the following
   post in the plugin’s blog:
 * [https://cff.dwbooster.com/blog/2019/01/06/columns/?index=1](https://cff.dwbooster.com/blog/2019/01/06/columns/?index=1)
 * The styles defined into the “Customize Form Design” attribute are applied to 
   the form’s preview too, but depends on the way the styles are defined.
 * Best regards.
 *  Thread Starter [hskokanek](https://wordpress.org/support/users/hskokanek/)
 * (@hskokanek)
 * [7 years, 3 months ago](https://wordpress.org/support/topic/display-conditional-text-in-calculation/#post-11329720)
 * Thanks a lot for this. It is extremely helpful.
 * One more thing I cannot figure out – how do you prevent the form from being sent/
   page reloaded upon hitting Enter/Return for example after filling in the first
   Number field.
 * The calculates all in real time, so there is no need to submit the form and most
   of the user will press Enter/Return in order to move onto the next field, which,
   however, causes the entire form to reload (= poor user experience :/)
 * Thanks – I am sure this will be somewhere in settings, but I cannot locate that.
 * BR,
    H.
 *  Plugin Author [codepeople](https://wordpress.org/support/users/codepeople/)
 * (@codepeople)
 * [7 years, 3 months ago](https://wordpress.org/support/topic/display-conditional-text-in-calculation/#post-11329828)
 * Hello [@hskokanek](https://wordpress.org/support/users/hskokanek/)
 * The pressing the key enter in the HTML standard makes the form submit. The alternative
   would be insert a “HTML Content” field in the form with the following piece of
   code as its content:
 *     ```
       <script>
       jQuery(document).on('keypress', '[id*="fieldlist"] :input', function(evt){if(evt.which == 13 || evt.which == 32) return false;});
       </script>
       ```
   
 * Best regards.

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘Display conditional text in calculation’ is closed to new replies.

 * ![](https://ps.w.org/calculated-fields-form/assets/icon-256x256.jpg?rev=1734377)
 * [Calculated Fields Form](https://wordpress.org/plugins/calculated-fields-form/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/calculated-fields-form/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/calculated-fields-form/)
 * [Active Topics](https://wordpress.org/support/plugin/calculated-fields-form/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/calculated-fields-form/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/calculated-fields-form/reviews/)

 * 10 replies
 * 2 participants
 * Last reply from: [codepeople](https://wordpress.org/support/users/codepeople/)
 * Last activity: [7 years, 3 months ago](https://wordpress.org/support/topic/display-conditional-text-in-calculation/#post-11329828)
 * Status: resolved