Javascript Element - using OR ||

kdiamond

Member
Could I please get a quick hand with this javascript? I have one id number that shows a field, the rest of the ID's hide the field. There are 6 total ID's.

Now I have the need for ID 1 OR 5 to show the field, and the rest will hide it. I am trying to use the || or syntax but when I do, the fields don't hid at all - they are showing and constant no matter what ID is selected.

var addState= document.getElementById('pet_profiles___state').getValue();
var element1= Fabrik.getBlock('form_10').formElements.get('pet_profiles___TABC');
var element2= Fabrik.getBlock('form_10').formElements.get('pet_profiles___TABC_Expire');
if (addState=='1'||addstate=='5')
{
element1.show();
element2.show();
}
else if (addState=='2'||addstate='3'||addstate='4'||addstate=='6')
{
element1.hide();
element2.hide();
}
 
Should work.

One suggestion is don't use ...

var addState= document.getElementById('pet_profiles___state').getValue();

... use ...

Fabrik.getBlock('form_10').formElements.get('pet_profiles___state').getValue();

Do you have this directly inline in a JS event, or do you have it wrapped up in a function?

I usually do the latter, as it makes it 10x easier to debug. So create a ./components/com_fabrik/js/X.js file (where X is the numeric form id). Put this in it ...

Code:
function doState(el) {
   var addState= el.form.formElements.get('pet_profiles___state').getValue();
   var element1= el.form.formElements.get('pet_profiles___TABC');
   var element2= el.form.formElements.get('pet_profiles___TABC_Expire');
   if (addState=='1'||addstate=='5')
   {
      element1.show();
      element2.show();
   }
   else if (addState=='2'||addstate='3'||addstate='4'||addstate=='6')
   {
      element1.hide();
      element2.hide();
   }
}

Then in your JS code box on the element, just do ...

doState(this);

Then in Chrome, open up the dev tools, look in Source tab, find form_X.js and put a breakpoint in the first line of that function. Then you can step through, look at variables, etc.

-- hugh
 
I just have to say THANK YOU for your example here of how to integrate a js file and the code related here.
It just significantly improved the functionality of one of my forms.

THANKS HUGH!!!!
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top