• Resolved davepurchaser

    (@davepurchaser)


    I have a form where a user is able to hit a button to show a dropdown menu. If they hit a different button, the first dropdown menu is hidden and a different one appears. I have different buttons that each use this logic.

    jQuery(‘.dropdown1’).show();
    jQuery(‘.dropdown2’).hide();
    jQuery(‘.dropdown3’).hide();

    etc.. This allows the user to browse one dropdown at a time by hitting a different button.

    The user is able to browse the dropdowns and eventually choose one of the items and continue to the next page.

    Now later in the form, I want to output the last item they selected in those dropdowns by using something like:

    jQuery(‘#result’).html(‘You selected ‘+LAST FIELD NAME SELECTED’);
    })()

    Note where it says “LAST FIELD NAME SELECTED” is where I am stuck. I don’t know how to reference the last field when

    a.) They fields don’t seem to be logically grouped together

    b.) The user may pick several different items from several dropdowns. How do I tell it to pick just the last one?

    Sorry if this is a bit of a complex question. Can I use a fieldset or div container for this?

    • This topic was modified 8 years, 2 months ago by davepurchaser.
Viewing 15 replies - 1 through 15 (of 25 total)
  • Plugin Author codepeople

    (@codepeople)

    Hello,

    As you don’t know what field was selected, or in what order, I propose you an alternative, assuming that the DropDown fields are: fieldname1, fieldname2, fieldname3, and fieldname4:

    – Insert a “HTML Content” field in the form with the piece of code below as its content:

    <script>
    jQuery(document).on(
    'change', 
    '[id*="fieldname1_"],[id*="fieldname2_"],[id*="fieldname3_"],[id*="fieldname4_"]', function(){
    jQuery('#result').html('You selected '+jQuery(this).val());
    });
    </script>

    and that’s all.
    Best regards.

    Thread Starter davepurchaser

    (@davepurchaser)

    Ahhh I’ve almost got it. This is driving me nuts.

    I’m trying to write the following

    <script>
    jQuery(document).on(
    ‘change’,
    ‘[id*=”fieldname1_”],[id*=”fieldname2_”],[id*=”fieldname3_”],[id*=”fieldname4_”]’, function(){
    jQuery(‘#result2’).html(‘You selected ‘+jQuery(this).val()+ ‘ at’ +(fieldname5)+ ‘ %’);
    });
    </script>

    The only thing I added to your script was +fieldname5+ and I can’t seem to find the right syntax to make it output. It should be able to grab another field in the output right?

    Please help.

    Plugin Author codepeople

    (@codepeople)

    Hello,

    The correct would be:

    <script>
    jQuery(document).on(
    'change', 
    '[id*="fieldname1_"],[id*="fieldname2_"],[id*="fieldname3_"],[id*="fieldname4_"]', function(){
    jQuery('#result2').html('You selected '+jQuery(this).val()+' at '+jQuery('[id*="fieldname5_"]').val()+'%');
    });
    </script>

    Best regards.

    Thread Starter davepurchaser

    (@davepurchaser)

    Ah so I’ve been trying to figure this out all night. fieldname5 in the above example is a number and it keeps returning a 0.

    fieldname5 is a calculated field with the below equation. It’s worth noting that it outputs the correct value in the calculated field, but when I try to insert it into my HTML content field, it always returns a 0.

    For example:

    You selected red with 0%

    prec((function(){
    var values = [fieldname2,fieldname6,fieldname73, fieldname74],
    total = 0,
    counter = 0;

    for(var i in values)
    {
    if(values[i]) counter++;
    total += values[i];
    }

    return (counter)?total/counter:0;
    })(),1)

    Plugin Author codepeople

    (@codepeople)

    Hello,

    If you want modify the text into the “result2” tag, when the value of fieldname5 changes, you should include it into the list of fields too:

    <script>
    jQuery(document).on(
    'change', 
    '[id*="fieldname1_"],[id*="fieldname2_"],[id*="fieldname3_"],[id*="fieldname4_"],[id*="fieldname5_"]', function(){
    jQuery('#result2').html('You selected '+jQuery(this).val()+' at '+jQuery('[id*="fieldname5_"]').val()+'%');
    });
    </script>

    Best regards.

    Thread Starter davepurchaser

    (@davepurchaser)

    Hi codepeople, this almost works!

    When I hit ‘calculate’ the first time, it still comes up as 0%. However, if I select a different option and calculate again, it outputs correctly.

    For some reason, the first time I calculate it still sits at 0%.

    • This reply was modified 8 years, 2 months ago by davepurchaser.
    Thread Starter davepurchaser

    (@davepurchaser)

    scratch this – thinking

    Thread Starter davepurchaser

    (@davepurchaser)

    So here’s how my form works.

    1.) I make a dropdown selection between fieldname1-4

    2.) On the next page I calculate the average in fieldname5

    3.) I output the selection in an HTML content field using the below logic (thanks very much for this. It mostly works wonderfully).

    <script>
    jQuery(document).on(
    ‘change’,
    ‘[id*=”fieldname1_”],[id*=”fieldname2_”],[id*=”fieldname3_”],[id*=”fieldname4_”],[id*=”fieldname5_”]’, function(){
    jQuery(‘#result2’).html(‘You selected ‘+jQuery(this).val()+’ at ‘+jQuery(‘[id*=”fieldname5_”]’).val()+’%’);
    });
    </script>;

    If I skip the dropdown (1-4) step in my form and calculate fieldname5 first, then back up and select dropdowns between 1-4, everything works properly. If I select between 1-4 first, then try to calculate fieldname5, fieldname5 comes back with 0% in my HTML content field. It’s worth noting that the calculated field calculates fine.

    Something about the way the code in the HTML content field evaluates the second step needs to be fixed because it seems like it’s dependent on fieldnames 1-4 and really I’m just calling it from a different calculated field.

    Can you help?

    Plugin Author codepeople

    (@codepeople)

    Hello,

    The code I sent you previously does not interfere with the equation associated to the calculated field: fieldname5, so, you should check this equation.

    However to be completely sure, return true at the end of code:

    <script>
    jQuery(document).on(
    'change', 
    '[id*="fieldname1_"],[id*="fieldname2_"],[id*="fieldname3_"],[id*="fieldname4_"],[id*="fieldname5_"]', function(){
    jQuery('#result2').html('You selected '+jQuery(this).val()+' at '+jQuery('[id*="fieldname5_"]').val()+'%');
    return true;
    });
    </script>

    Best regards.

    Thread Starter davepurchaser

    (@davepurchaser)

    I think it’s a triggering issue though. When I hit “Calculate” it triggers this equation

    prec((function(){
    var values = [fieldname2,fieldname6,fieldname73, fieldname74],
    total = 0,
    counter = 0;

    for(var i in values)
    {
    if(values[i]) counter++;
    total += values[i];
    }

    return (counter)?total/counter:0;
    })(),1)

    … and outputs it to fieldname5. But in my HTML output which looks at

    <script>
    jQuery(document).on(
    ‘change’,
    ‘[id*=”fieldname1_”],[id*=”fieldname2_”],[id*=”fieldname3_”],[id*=”fieldname4_”],[id*=”fieldname5_”]’, function(){
    jQuery(‘#result2’).html(‘You selected ‘+jQuery(this).val()+’ at ‘+jQuery(‘[id*=”fieldname5_”]’).val()+’%’);
    return true;
    });
    </script>

    It seems to be waiting for me to go back to fieldnames 1-4 to pick it again. It won’t evaluate the output of the calculation in real time. It wants me to go back and pick a dropdown before it will evaluate it.

    For example, if I calculate fieldname5 first, THEN pick between fields 1-4, it works fine.

    • This reply was modified 8 years, 2 months ago by davepurchaser.
    Thread Starter davepurchaser

    (@davepurchaser)

    Am I wrong in thinking that the equation only evaluates fieldname 1-4 when there’s a change in the dropdowns, and the reason it’s not evaluating fieldname5 is because the equation begins by asking for something (evaluate a change in fieldname 1-4) that isn’t happening?

    If I simply reversed the logic..

    1.) Grab an average and output it to fieldname5

    2.) Evaluate the last field chosen in dropdowns 1-4

    3.) Output fieldname1-4 + fieldname 5 on calculate

    This should work. In fact, it does work. I just don’t know how to change the code to run fieldname5 first.

    Plugin Author codepeople

    (@codepeople)

    Hello,

    Could you send me the link to your webpage, please? Because only with your description I cannot check the values of fields, and the evaluation of the equation.

    Best regards.

    Thread Starter davepurchaser

    (@davepurchaser)

    Certainly. Thanks so much for having a look. It is here
    https://143.95.252.125/~chadtesting/about/

    1.) select school (you can ignore e-mail)
    2.) Pick a faculty
    3.) Pick a course
    4.) Put in a grade (you only need one)
    5.) Calculate

    notice output is 0%

    Now if you hit “Previous” and re-do step 3, then step through it again, it works fine.

    Plugin Author codepeople

    (@codepeople)

    Hello,

    The issue is simple. As you have disabled the dynamic evaluation of the equations, the onchange event is not triggered after calculate the value of the fieldname4 field.

    Please, modify the equation associated to fieldname4 as follows:

    prec((function(){
    var values = [fieldname2,fieldname6,fieldname73, fieldname74],
    total = 0,
    counter = 0;
    for(var i in values)
    {
    if(values[i]) counter++;
    total += values[i];
    }
    setTimeout(function(){jQuery('[id*="fieldname4_"]').change();},500);
    return (counter)?total/counter:0;
    })(),1)

    I’m sorry, but the support service does not cover the implementation of the users’ forms and formulas, if you need additional help implementing your project, I can offer you a custom coding service from my personal website:

    https://cff.dwbooster.com/customization

    Best regards.

    Thread Starter davepurchaser

    (@davepurchaser)

    Hey, thanks for bearing with me.

    I get what you’re trying to do. You want to re-evaluate the onchange event on calculate, so that it picks up fieldname4. That is on the right track but unfortunately it just freezes my window.

Viewing 15 replies - 1 through 15 (of 25 total)
  • The topic ‘Question about referencing last button pressed on a page’ is closed to new replies.