Skip to main content
Inspiring
June 7, 2022
Answered

Javascript drop down menu selection calls a function

  • June 7, 2022
  • 2 replies
  • 769 views

Hi all,

I'm in over my head once again. Currently trying to learn JavaScript and trying to run before I can walk. Hey ho.

How do I go about calling one of three different functions based on the user's selection from a drop-down menu?

I can only seem to get it to do A4. And it also runs the function when I click cancel (which it shouldn't do).

Any help, as always, is very much appreciated.

 

var myList = ["A4", "US Letter", "A4 & US Letter"];
var myDialog = app.dialogs.add({name:"PDF Export", canCancel:true});
with (myDialog )
    with (dialogColumns.add())
        with(borderPanels.add())
            {
            staticTexts.add ({staticLabel:"Select:"});
            var myDropSelection = dropdowns.add({stringList:myList, selectedIndex:2});
            }
if(myDialog.show() == true){
  alert (myList [myDropSelection.selectedIndex])
  myDialog.destroy();
  }

if([selectedIndex = 0]){
        exportA4();
}

if([selectedIndex = 1]){
        exportUS();
}

if([selectedIndex = 2]){
        exportBoth();
}
 

function exportA4() {//code here}

function exportUS() {//code here}

function exportBoth() {//code here}

 

This topic has been closed for replies.
Correct answer brian_p_dts

A few errors with the code. When comparing things, use the == comparator, not =. Also, no need to have those items in brackets. Good to store the selection during show then check that selection after destroying. 

var myList = ["A4", "US Letter", "A4 & US Letter"];
var myDialog = app.dialogs.add({name:"PDF Export", canCancel:true});
with (myDialog )
    with (dialogColumns.add())
        with(borderPanels.add())
            {
            staticTexts.add ({staticLabel:"Select:"});
            var myDropSelection = dropdowns.add({stringList:myList, selectedIndex:2});
            }
var mySel;
if(myDialog.show()){
    mySel = myDropSelection.selectedIndex;
    myDialog.destroy();
  }

if(mySel == 0){
        exportA4();
}

if(mySel == 1){
        exportUS();
}

if(mySel == 2){
        exportBoth();
}
 

function exportA4() { alert("Export A"); }

function exportUS() { alert("Export US"); }

function exportBoth() { alert("Export Both"); }

 

2 replies

brian_p_dts
Community Expert
brian_p_dtsCommunity ExpertCorrect answer
Community Expert
June 7, 2022

A few errors with the code. When comparing things, use the == comparator, not =. Also, no need to have those items in brackets. Good to store the selection during show then check that selection after destroying. 

var myList = ["A4", "US Letter", "A4 & US Letter"];
var myDialog = app.dialogs.add({name:"PDF Export", canCancel:true});
with (myDialog )
    with (dialogColumns.add())
        with(borderPanels.add())
            {
            staticTexts.add ({staticLabel:"Select:"});
            var myDropSelection = dropdowns.add({stringList:myList, selectedIndex:2});
            }
var mySel;
if(myDialog.show()){
    mySel = myDropSelection.selectedIndex;
    myDialog.destroy();
  }

if(mySel == 0){
        exportA4();
}

if(mySel == 1){
        exportUS();
}

if(mySel == 2){
        exportBoth();
}
 

function exportA4() { alert("Export A"); }

function exportUS() { alert("Export US"); }

function exportBoth() { alert("Export Both"); }

 

JustyRAuthor
Inspiring
June 8, 2022

Thanks, guys. Both of your suggestions worked perfectly.

Kasyan Servetsky
Legend
June 8, 2022
quote

Thanks, guys. Both of your suggestions worked perfectly.


By @JustyR

Great minds think alike! 🙂

Kasyan Servetsky
Legend
June 7, 2022

It was almost 20 years ago the last time I used the dialogs object. 🙂 I guess it should be something like that:

var myList = ["A4", "US Letter", "A4 & US Letter"];
var myDialog = app.dialogs.add({name:"PDF Export", canCancel:true});
with (myDialog )
    with (dialogColumns.add())
        with(borderPanels.add())
            {
            staticTexts.add ({staticLabel:"Select:"});
            var myDropSelection = dropdowns.add({stringList:myList, selectedIndex:2});
            }
if(myDialog.show() == true){
	
	if(myDropSelection.selectedIndex == 0){
			exportA4();
	}
	else if(myDropSelection.selectedIndex == 1){
		exportUS();
	}
	else if(myDropSelection.selectedIndex == 2){
		exportBoth();
	}

	myDialog.destroy();
}

function exportA4() {
	$.writeln(arguments.callee.toString().match(/function ([^\(]+)/)[1]);
}

function exportUS() {
	$.writeln(arguments.callee.toString().match(/function ([^\(]+)/)[1]);
}

function exportBoth() {
	$.writeln(arguments.callee.toString().match(/function ([^\(]+)/)[1]);
}

 

m1b
Community Expert
Community Expert
June 8, 2022

Hi @Kasyan Servetsky, just a little thing I noticed in passing... functions have a name property so you should be able to simply to arguments.callee.name. Tiny thing but I like it when people tell me these things so hope you don't mind either.

- Mark