This article describes the implementation methods of listbox selection and anti-selecting of JavaScript listbox. Share it for your reference. The specific analysis is as follows:
Selecting all and unselecting list boxes through JS code is often necessary and is very practical.
function listboxSelectDeselect(listID, isSelect) { var listbox = document.getElementById(listID); for(var count=0; count < listbox.options.length; count++) { listbox.options[count].selected = isSelect; }}Below is a detailed usage example
Click below buttons to select or deselect all options from select box<br> <select id="lsbox" name="lsbox" size="10" multiple=""> <option value="1">India</option> <option value="2">United States</option> <option value="3">China</option> <option value="4">Italy</option> <option value="5">Germany</option> <option value="6">Canada</option> <option value="7">France</option> <option value="8">United Kingdom</option> </select> <br><button onclick="listboxSelectDeselect('lsbox', true);">Select All</button><button onclick="listboxSelectDeselect('lsbox', false);">Deselect All</button><script>function listboxSelectDeselect(listID, isSelect) { var listbox = document.getElementById(listID); for(var count=0; count < listbox.options.length; count++) { listbox.options[count].selected = isSelect; }}</script>I hope this article will be helpful to everyone's JavaScript programming.