Skip to main content
Shulipa Bernad
Inspiring
February 2, 2021
Answered

Removing items from the drop-down list

  • February 2, 2021
  • 2 replies
  • 508 views

Hello guys! How do I remove the selected item from a drop-down list?
Here is a small script with an example, I don't understand why it doesn't work. Grateful for the help.

 

 

 

var dlg = new Window("dialog"); 
var conteudo = ["Item 1","Item 2","Item 3","Item 4","Item 4","Item 4","Item 4","Item 4","Item 4"]; 
var list = dlg.add("dropdownlist", undefined, undefined, {name: "list", items: conteudo}); 
list.selection= 0;
var remove = dlg.add("button", undefined, undefined, {name: "remove"}); remove.text="Remove ítem"

remove.onClick = function () {
     for (var i = list.selection.length-1; i > -1; i--)
     list.remove (list.selection[i]);
}

dlg.show();

 

 

 

 

 
 
This topic has been closed for replies.
Correct answer Kukurykus
win = new Window('dialog')
lst = win.add('dropdownlist', [0, 0, 100, 20], [1, 2, 3])
btn = win.add('button', undefined, 'Delete an item')
btn.onClick = function() {lst.remove(lst.selection)} win.show()

2 replies

Legend
February 3, 2021

Here is a small script with an example, I don't understand why it doesn't work. Grateful for the help.

 

Because instead of

 

remove.onClick = function () {
     for (var i = list.selection.length-1; i > -1; i--)
     list.remove (list.selection[i]);
}

 

use

 

remove.onClick = function () {
     for (var i = list.items.length-1; i > -1; i--)
     list.remove (list.items[i]);
}

 

: )

Kukurykus
Legend
February 3, 2021

It's going to remove all items, while he wants only one, the selected I think. For all there's removeAll()

Kukurykus
KukurykusCorrect answer
Legend
February 2, 2021
win = new Window('dialog')
lst = win.add('dropdownlist', [0, 0, 100, 20], [1, 2, 3])
btn = win.add('button', undefined, 'Delete an item')
btn.onClick = function() {lst.remove(lst.selection)} win.show()
Shulipa Bernad
Inspiring
February 2, 2021

Perfect! Thank you for your help Kukurykus .