• Resolved mihaibalan

    (@mihaibalan)


    Hello! I’ve been having real trouble with creating a Grade calculator for a medical school. I know it’s a bit ambitious for a form builder but it’s been going really well until this last step that doesn’t work for the life of me.
    So the form consists of many “select” dropdowns, where students can select their grades, afterwards, they go in calculators, where they are multiplied by their credits (aka. their weight) and divided by the total credits on the semester/year.
    I’m using a php/js code that allows me to save the filled form locally and fill back the form automatically once the user is back on the site without them having to refill it.
    Here is the code i used for that:
    https://codefile.io/f/PR1Gq2cfuY

    The problem is that there are certain classes that are either in the first or the second semester, so students need to choose which semester to add that class in, which I managed to do through conditional “Hide ifs”.
    The problem is that after automatically filling the forms when the page loads, it does not register the semester choices, which puts all the swapable classes in the first semester and I have to choose semesters again for the form to function properly.
    Could anyone please help me figure out a way to make the form identify those hideifs properly? Is there any way to make the form aware of those conditional logic once the form is automatically filled? Some sort of refresh function?

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Thread Starter mihaibalan

    (@mihaibalan)

    Hi everyone! I’ve figured it out by myself! Let me explain how I’ve done it: I’m not a professional developer so please excuse me if my language isn’t professional but I’ll do my best to explain it clearly. Maybe it will help somebody else!

    So Forminators conditional logic works based on javascript eventlisteners that are waiting for a “change” event to call their linked function.
    Filling the forms with a javascript form does not count as an event, so the conditional logic never fires even if the form is filled.

    I’ve written this bit of code to fix it:

    //define variables
    const e = new Event(“change”)
    var select = document.getElementsByClassName(“forminator-select–field forminator-select2”);

    // define dispatchevent function
    function fixselect(f){
    f.dispatchEvent(e)
    }
    //for loop through all selects
    for (var i = 0; i < select.length; i++) {
    fixselect(select[i]);
    }

    Technically what happens is that all of the select fields have this class name:
    “forminator-select–field forminator-select2”
    So I define a variable that selects all of the select fields, the problem is that this creates a HTML list, that is why we have to use a for loop in order to dispatch individual events on each selected element, doing so on document, window or the HTML list does not work.

    The one in the middle just creates the dispatchEvent() function that is called in the for loop,which triggers a change event (the “e” variable) manually.

    Hope this will help someone, I’m super happy with how it worked out.

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.