• Resolved chrisonga

    (@chrisonga)


    Hi All,

    I have a dropdown field to select a certain ‘course’.
    Based on which course is selected in the dropdown I would like to populate a single line text field below with a short description.

    Is there a way to do this (conditionally) without having to create a bunch of dependant fields?

    Thanks in advance for any help.

    Regards
    Chris

    https://wordpress.org/plugins/calculated-fields-form/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author codepeople

    (@codepeople)

    Hi Chris,

    In this case you can use a calculated field, and conditional statements in the equation to identify the choice selected. For example, suppose that the fieldname1 field is a DropDown field with the courses names: Course A, Course B, and Course C, and then in the equation you can check the choice selected, and return the corresponding description:

    (function(){
    var f1 = fieldname1;
    
    if(f1 == 'Course A') return 'Description for the Course A';
    if(f1 == 'Course B') return 'Description for the Course B';
    if(f1 == 'Course C') return 'Description for the Course C';
    })()

    or even better:

    (function(){
    var f1 = fieldname1;
    
    switch(f1){
    case 'Course A': return 'Description for the Course A'; break;
    case 'Course B': return 'Description for the Course B'; break;
    case 'Course C': return 'Description for the Course C'; break;
    }
    })()

    Best regards.

    Thread Starter chrisonga

    (@chrisonga)

    Thanks for the great response!
    switch/case works a treat.

    One last question on this.
    I have number values on some of the dropdowns as I have to calculate costs of the courses etc. I have noticed that the ‘case’ is reading the ‘value’ and not the ‘text’ part of the dropdown. Is it possible that the ‘case’ can read the ‘text’ instead of the ‘value’?

    Regards and thanks again,
    Chris

    Plugin Author codepeople

    (@codepeople)

    Hi,

    In this case you should apply some tricks in the equation:

    1. Assigns a class name to the DropDown field (for example: my-field)

    Note: The class names are assigned to a field through the attribute: “Add Css Layout Keywords”

    2. Modify the equation as follows:

    (function(){
    var tmp = fieldname1;
    var f1 = jQuery('.my-field option:selected').attr('vt');
    
    switch(f1){
    case 'Course A': return 'Description for the Course A'; break;
    case 'Course B': return 'Description for the Course B'; break;
    case 'Course C': return 'Description for the Course C'; break;
    }
    })()

    The first line in the equation has not a direct use (var tmp = fieldname1;) however, this line of the code indicates to the plugin that the equation should be evaluated each time that is selected a different option in the fieldname1 field.

    Best regards.

    Thread Starter chrisonga

    (@chrisonga)

    Thanks again for the great support.
    Will try this out.
    Regards
    Chris

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Populate text field depending on dropdown selection’ is closed to new replies.