Selecting and deselecting list box item :- JavaScript


  • getElementById is used to select the HTML element by its id value.
  • element.options.length is used to get the length of the list items.
  • In the for loop every execution, listbox.options[i].selected is used to check or uncheck the list items.

Code:-

  • <!DOCTYPE html> 
    1. <html> 
    2.  
    3. <head> 
    4.  <meta name="viewport" content="width=device-width, initial-scale=1"> 
    5.  
    6.  <script> 
    7.   function listboxSelectDeselect(isSelect) 
    8.   { 
    9.    var listbox = document.getElementById("lstid"); 
    10.    for (var i = 0; i < listbox.options.length; i++) 
    11.    { 
    12.     listbox.options[i].selected = isSelect; 
    13.    } 
    14.   } 
    15.  </script> 
    16.  
    17. </head> 
    18.  
    19. <body> 
    20.  
    21.  <select id="lstid" size="15" multiple=""> 
    22.   <option value="1">Merbin</option> 
    23.   <option value="2">Franklin</option> 
    24.   <option value="3">Jose</option> 
    25.   <option value="4">Geetha</option> 
    26.   <option value="5">Jino</option> 
    27.   <option value="6">Ganesh</option> 
    28.   <option value="7">Kumar</option> 
    29.   <option value="8">Reegan</option> 
    30.   <option value="9">Rajesh</option> 
    31.  </select> 
    32.  <br> 
    33.  <button onclick="listboxSelectDeselect(true);">Select All</button> 
    34.  <button onclick="listboxSelectDeselect(false);">Deselect All</button> 
    35.  
    36. </body> 
    37.  
    38. </html>


    Selecting and deselecting list box item :- JavaScript Selecting and deselecting list box item :- JavaScript Reviewed by Network security on November 13, 2019 Rating: 5

    No comments:

    Useful Information

    Powered by Blogger.