Skip to main content
smithcgl9043167
Inspiring
October 26, 2020
Answered

Removing and printing items from the text box.

  • October 26, 2020
  • 1 reply
  • 465 views

Greetings everyone!
After a few months of pausing in my studies, I decided to return and soon I ran into an error in the script I picked up on this one on page 31 of this manual: https://adobeindd.com/view/publications/a0207571-ff5b-4bbf-a540-07079bd21d75/92ra/publication-web-resources/pdf/scriptui-2-16-j.pdf 

My question is: How to return only the items in the text box after deleting some? It prints all items even after deleting. Where's the error?
Thank you

 

 

 

 

 

var w = new Window ("dialog");
var array= ["one", "two", "three", "four"]
var lbx = w.add ("listbox", undefined, array, {multiselect: true});
var del = w.add ("button", undefined, "Delete selected items");

del.onClick = function () {
     // remember which line is selected
     var sel = lbx.selection[0].index;
     for (var i = lbx.selection.length-1; i > -1; i--)
     lbx.remove (lbx.selection[i]);
     // select a line after deleting one or more items
     if (sel > lbx.items.length-1)
     lbx.selection = lbx.items.length-1;
     else
     lbx.selection = sel;
}

var print = w.add ("button", undefined, "Print selected items");
    print.onClick = function () {
     for (var i = 0; i < array.length; i++)
     alert(array[i]);
}
w.show ();

 

 

 

 

 

 
This topic has been closed for replies.
Correct answer r-bin
It is necessary to remove elements from the array at the same time as removing the list elements.

Or don't use an array, but use lbx.items.
var print = w.add ("button", undefined, "Print selected items"); // why selected???
    print.onClick = function () {
     for (var i = 0; i < lbx.items.length; i++)
     alert(lbx.items[i].text);
}
 

1 reply

r-binCorrect answer
Legend
October 26, 2020
It is necessary to remove elements from the array at the same time as removing the list elements.

Or don't use an array, but use lbx.items.
var print = w.add ("button", undefined, "Print selected items"); // why selected???
    print.onClick = function () {
     for (var i = 0; i < lbx.items.length; i++)
     alert(lbx.items[i].text);
}
 
smithcgl9043167
Inspiring
October 26, 2020

Great, it worked now! Thank you r-bin , I am happy to know that you still attend this community.