To delete multiple items from the list box at the same time, you can only delete them from the bottom up, so that the index number will not change. The following is a good example, you can feel that you want to delete multiple projects from the list box at the same time. We cannot delete them from top to bottom, because the index number of the following items changes every time you delete one of the items above, therefore, you can only delete it from the bottom up, so that the index number will not change.
Html code
The Code is as follows:
India United States China Italy Germany Canada France United Kingdom
|
Delete Reset
|
The javascript code is as follows:
The Code is as follows:
Function listbox_remove (sourceID ){
// Get the listbox object from id.
Var src = document. getElementById (sourceID );
// Iterate through each option of the listbox
For (var count = src. options. length-1; count> = 0; count --){
// If the option is selected, delete the option
If (src. options [count]. selected = true ){
Try {
Src. remove (count, null );
} Catch (error ){
Src. remove (count );
}
}
}
}
Of course, if jQuery is used for deletion, it will be convenient and it will be done in one sentence.
The Code is as follows:
$ ("# SourceId"). find ('option: selected'). remove ();