1. Fabrik 3.9 has been released. If you have updated Joomla to 3.9, this is a required update.
    Dismiss Notice

Declaring Global variables in form_x.js

Discussion in 'Community' started by ontarget, Dec 6, 2018.

  1. ontarget

    ontarget Member

    Level: Community
    Hi I have a bunch of validations running through form_1.js
    Since upgrading to joomla 3.9.x and Fabrik 3.9 i seem to be having issues with js validations even basic onchange show / hide stuff
    I'm thinking that my js file is rather messy with form elements being repeated in various functions - I would like to tidy up my work and make all the repeated variables used locally global instead - I think i then have a better chance of fixing my validation issues.
    Is this the correct method?

    Code (Javascript):
    var modeTransport = Fabrik.getBlock('form_1').formElements.get('aaa_participant_claim___mode_of_transport');
    var publicTransport = Fabrik.getBlock('form_1').formElements.get('aaa_participant_claim___public_transport');
    var stationEircode = Fabrik.getBlock('form_1').formElements.get('aaa_participant_claim___station_eircode');

    function showPublic(){

    if (modeTransport.getValue() === 'car') {
    publicTransport.hide();
            stationEircode.hide();
      }
    }
    My old javascript was

    Code (Javascript):
    function showPublic(el){
           var elTransport2 = el.form.formElements.get('aaa_participant_claim___mode_of_transport');
        var elPublic2 = el.form.formElements.get('aaa_participant_claim___public_transport');
     
        var elEircodeStat = el.form.formElements.get('aaa_participant_claim___station_eircode');
     
         if (elTransport2.getValue() === 'car') {
             elPublic2.hide();
           
            elEircodeStat.hide();
        }
    }
     
  2. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    Your new way probably won't work, because the form blocks and elements object probably win't have finished initializing before you call Fabrik.getBlock() .... remember that Javascript is asyncronous.

    Your old method is perfectly fine, really doesn't matter having el.form.formElements.get() repeated in different functions.

    Have you tried sticking breakpoints in your code in the browser dev tools to get a sense of what's going on?

    -- hugh
     

Share This Page