• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Create Global Array for Form validation

New Here ,
Nov 23, 2021 Nov 23, 2021

Copy link to clipboard

Copied

I'm trying to create an array that I can use for multiple validation scripts.  I have a long list of fields that are set to "readonly = true".  I have successfully created an array and loaded all of the fields needed. 

var AdminFields = new Array(38);

AdminFields[0] ="formField1";
AdminFields[1] ="formField2";

etc.....

My problem is that I can only get the array to work locally.  If I try to reference the array from another button I get a "ReferenceError: AdminFields is not defined".  The only way to get the button to work is to list the entire array again in the script for that button.

I tried loading a document level function to create the array.  I called the function "initAdminFields()".  I thought this would allow me to reference "initAdminFields()" at the top of my script so I wouldn't have to recreate the array again.  Didn't work.

 

Q: Is there a way to create a global array that can be used throughout a form?

Q: Where do you create a global array?

Q: How do you call the array in a script (like one for a button mouse up event)?

 

Thank you in advance.  I am a casual javascript user.  I appreciate everyone's patience as I grow my knowledge.

TOPICS
How to , Scripting

Views

301

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Participant , Nov 23, 2021 Nov 23, 2021

Hi what does "create a global array" mean? Try creating a variable outside of the function.

 

function foo(){
	// local
	var a = 'a'
	//global
	b = 'b'
}

// global
var c = 'c'
// global
d = 'd'

 

 

 

Votes

Translate

Translate
Participant ,
Nov 23, 2021 Nov 23, 2021

Copy link to clipboard

Copied

Hi what does "create a global array" mean? Try creating a variable outside of the function.

 

function foo(){
	// local
	var a = 'a'
	//global
	b = 'b'
}

// global
var c = 'c'
// global
d = 'd'

 

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 24, 2021 Nov 24, 2021

Copy link to clipboard

Copied

Thank you for the reply.

 

By "global array" I mean an array that I can load when the form loads.  One that is persistent as long as the form is open and can be referenced in scripts later in the form.

 

I will try your example.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 24, 2021 Nov 24, 2021

Copy link to clipboard

Copied

LATEST

I made corrections to my code to remove "var" from the line statement.  It worked.  

Thank you for the quick rely.

codeSample.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 24, 2021 Nov 24, 2021

Copy link to clipboard

Copied

Use a target engine. At the top of a script add this line:

#targetengine anystringwilldo;

var AdminFields = new Array(38);

AdminFields[0] ="formField1";
AdminFields[1] ="formField2";

 

This creates a so-called persistent variable, one that persists after the script finishes but in this engine only. Now any other script that uses the same engine has access to the variable (as Sychev demonstrated in another thread only yesterday:))

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines