Skip to main content
Simonetos The Greek
Inspiring
July 9, 2019
Answered

Alert selected index of a dropdownlist at onChange event

  • July 9, 2019
  • 1 reply
  • 1166 views

I am trying to alert the selected index of a dropdownlist, for example 0 for Name 1, 1 for Name 2 etc, but instead of this I get an undefined value... Any idea what am I doing wrong?

var dropdownlist1_array = ["Name 1","Name 2","Name 3"]; 
var dropdownlist1 = panel1.add("dropdownlist", undefined, dropdownlist1_array);
  dropdownlist1.selection = 0;
  dropdownlist1.text = "Type:";
  dropdownlist1.onChange = function(){var index = dropdownlist1.selectedIndex; alert(index); };
This topic has been closed for replies.
Correct answer SuperMerlin

var panel1 = new Window("dialog","test");

var dropdownlist1_array = ["Name 1","Name 2","Name 3"];

var dropdownlist1 = panel1.add("dropdownlist", undefined, dropdownlist1_array);  

  dropdownlist1.selection = 0;  

  dropdownlist1.text = "Type:"; 

  dropdownlist1.onChange = function(){ alert(this.selection.text + " Index = " + this.selection.index); };

  panel1.show();

1 reply

SuperMerlin
SuperMerlinCorrect answer
Inspiring
July 9, 2019

var panel1 = new Window("dialog","test");

var dropdownlist1_array = ["Name 1","Name 2","Name 3"];

var dropdownlist1 = panel1.add("dropdownlist", undefined, dropdownlist1_array);  

  dropdownlist1.selection = 0;  

  dropdownlist1.text = "Type:"; 

  dropdownlist1.onChange = function(){ alert(this.selection.text + " Index = " + this.selection.index); };

  panel1.show();

Simonetos The Greek
Inspiring
July 9, 2019

Thank you very much for the fast answer and for your time!!!