How to delete multiple project_javascript from listbox at the same time-js tutorial

Source: Internet
Author: User
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 ();

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.