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

Dropdown list function and variables

Community Beginner ,
Apr 25, 2017 Apr 25, 2017

Copy link to clipboard

Copied

Hi

​I have a basic problem with dropdown list.

my variable alignTxt doesn't work outside function.

​var alignmentBox = alignmentBoxGroup.add("dropdownlist", undefined, ["Left","Center", "Right"]);

​

​    alignmentBox.onChange = function (){

        if(this.selection.index==0){

            var alignTxt = "ParagraphJustification.LEFT_JUSTIFY";

        };

        if(this.selection.index==1){

            var alignTxt = "ParagraphJustification.CENTER_JUSTIFY";

        };

        if(this.selection.index==2){

            var alignTxt = "ParagraphJustification.RIGHT_JUSTIFY";

        };

}

​

​myText.justification = alignTxt;

​

Thanks in advance!

TOPICS
Scripting

Views

421

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
Enthusiast ,
Apr 25, 2017 Apr 25, 2017

Copy link to clipboard

Copied

The scope of a variable is limited to the function in which it is defined, so you just need to declare the variable outside the function:

var alignTxt;

Then not have the var where the variables are used inside the function.

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 Beginner ,
Apr 25, 2017 Apr 25, 2017

Copy link to clipboard

Copied

Thank you for your fast response Paul.

I've tried it earlier but it doesn't work.

I'm getting undefined on alert(alignTxt);

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
Enthusiast ,
Apr 25, 2017 Apr 25, 2017

Copy link to clipboard

Copied

Well you definitely have an issue with variable scope (limited to that onChange function) in the above example, and it would probably still return undefined until it had gone through your onChange function at least once, unless when you declare it initially you give it a default value.

Put in lots of $.writeln(alignTxt) type stuff throughout your code to keep track of things by checking the results in the ESTK javascript console (make sure you've enabled javascript debugger in the AE prefs).

If you still can't figure it out you'll probably have to post more code so we can see the whole thing in action.

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 Beginner ,
Apr 25, 2017 Apr 25, 2017

Copy link to clipboard

Copied

LATEST

Thank you for your time Paul.

I've solved it.

I should remove quotation marks from variables

and set var alignTxt = ParagraphJustification.LEFT_JUSTIFY; as default variable.

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