Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • jQuery Tutorial
  • jQuery Selectors
  • jQuery Events
  • jQuery Effects
  • jQuery Traversing
  • jQuery HTML & CSS
  • jQuery AJAX
  • jQuery Properties
  • jQuery Examples
  • jQuery Interview Questions
  • jQuery Plugins
  • jQuery Cheat Sheet
  • jQuery UI
  • jQuery Mobile
  • jQWidgets
  • Easy UI
  • Web Technology
Open In App
Next Article:
How to Select Multiple Options at once in Dropdown list in HTML?
Next article icon

How to select/deselect multiple options in dropdown using jQuery ?

Last Updated : 06 Jun, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

To select and deselect multiple options in a drop-down menu, we will use the HTML and CSS codes. HTML code helps to define the basic structure of the webpage and CSS will benefit in designing the web page.

Some essential properties used in the code are as follows-

  • <div> - This is a division tag that helps us to define a particular section of the HTML code to be referred to and can be edited easily later in the CSS style sheet.
  • <link rel="stylesheet" href="> - This tag contains the link to the CSS style sheet created by us and inherits all the modifications from the stylesheet which we want to make to our webpage.
  • container-fluid - This class provides a full-width container that extends through the website.
  • <div class="row"> - This is a bootstraps grid system and helps us to fill up to 12 rows across a page.
  • mul-select - This class allows us to choose from the various options enlisted in the drop-down menu.

Approach: This is a simple HTML code to display a drop-down menu in a website where we can select multiple data-structures options and deselect others using the multi-select option.

We will be using Bootstrap here to use Bootstrap's responsive grid system and form control. Below is the code to include the Bootstrap CDN link in the head section of the HTML code.

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">

Example: 
 

HTML
<!DOCTYPE html> <html lang="en">  <head>     <meta charset="UTF-8">     <meta name="viewport" content=         "width=device-width, initial-scale=1.0">      <!-- These are the jQuery extensions taken from         bootstrap website to enable the drop down         function of the menu bar -->     <link rel="stylesheet" href= "https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/css/select2.min.css">      <script src= "https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js">     </script>      <script src= "https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/js/select2.min.js">     </script>      <!-- Default bootstrap CSS link taken from the         bootstrap website-->     <link rel="stylesheet" href= "https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">      <script src= "https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js">     </script>      <style>         *{             margin: 0; padding: 0; box-sizing: border-box;         }         body{             justify-content: center;             align-items: center;             min-height: 100%;         }         .form{             margin: 2% 20%;         }         .mul-select {             min-width: 100%;         }                  h1 {             color: #fff;         }     </style> </head>  <body>     <div class="container-fluid bg-dark" style="min-height: 50vh;">         <div class="text-center">             <h1 class="pt-4" style="color: #29c94f;">GeeksforGeeks</h1>             <h1 class="py-4">Select Multiple Options</h1>         </div>          <!-- These modifications are done to make the webpage             adaptive to the screen size and automatically             reduce the size when screen is minimized -->         <div class="row justify-content-center align-items-center">             <div class="col-lg-6 col-md-6 col-12">                 <div class="form-group form">                      <!-- Various options in drop down menu to                         select the types of data structures                         that we need -->                     <select class="mul-select" multiple="true">                         <option value="Stack">Stack</option>                         <option value="Queue">Queue</option>                         <option value="Linked-List">Linked-List</option>                         <option value="Heap">Heap</option>                         <option value="Binary-Tree">Binary-Tree</option>                         <option value="Graph">Graph</option>                         <option value="Array">Array</option>                     </select>                 </div>             </div>         </div>     </div>      <!-- JavaScript code to enable the drop down function -->     <script>         $(document).ready(function() {             $(".mul-select").select2({                 placeholder: "select data-structures",                 tags: true,             });         })     </script> </body>  </html> 

In the HTML code, we have also included some JavaScript codes because, in a web page, we have to use JavaScript for any type of function to be performed.

 

As we can see on opening the file in a browser this is the output obtained and we are getting the desired drop-down bar

 

This shows that we are able to select multiple options at a single time just by clicking on them. Apart from that, we can also observe that all the options which are chosen have been shaded as grey.

To unselect the selected options- we can simply click on the cross button near the text and the option will be automatically unselected also the grey shade in the drop-down menu has been removed. The following output gif demonstrates the same:

 

Source to download the Bootstrap, CSS and JavaScript extensions- https://getbootstrap.com/docs/4.5/getting-started/introduction/

HTML is the foundation of web pages and is used for webpage development by structuring websites and web apps. You can learn HTML from the ground up by following this HTML Tutorial and HTML Examples.

CSS is the foundation of web pages and is used for webpage development by styling websites and web apps. You can learn CSS from the ground up by following this CSS Tutorial and CSS Examples.

jQuery is an open-source JavaScript library that simplifies the interactions between an HTML/CSS document, It is widely famous for its philosophy of “Write less, do more”.
You can learn jQuery from the ground up by following this jQuery Tutorial and jQuery Examples.


Next Article
How to Select Multiple Options at once in Dropdown list in HTML?

D

debanshupanda2001
Improve
Article Tags :
  • Web Technologies
  • JQuery
  • jQuery-Questions

Similar Reads

  • How to detect if a dropdown list is a multi-select using jQuery?
    In this article, we will learn how to detect if a dropdown list or an HTML select element is a multi-select one or not using jQuery. There is one main approach with which this can be implemented. Approach: Using the prop() method in the jQuery library. There is one select element with an id of dropd
    3 min read
  • How to select multiple elements using jQuery ?
    In this article, we will learn how to select multiple elements using JQuery. JQuery is the fastest and most lightweight JavaScript library that is used to simplify the interactions between an HTML/CSS document, or more precisely the Document Object Model (DOM), and JavaScript. JQuery is widely famou
    2 min read
  • How to create a Disabled Option Select using jQuery Mobile ?
    jQuery Mobile is a web-based technology used to make responsive content that can be accessed on all smartphones, tablets, and desktops. In this article, we will be creating a Disabled Option Select using jQuery Mobile. Approach: First, add jQuery Mobile scripts needed for your project. <link rel=
    1 min read
  • How to Select Multiple Options at once in Dropdown list in HTML?
    Dropdown lists are one of the most flexible elements in HTML. It is similar to that of the radio input, that is, only one item can be selected from a group of items by default. However, when the multiple attribute is used with the <select> element, we can enable the selection of multiple optio
    3 min read
  • How to remove options from select element using jQuery ?
    Removing options from a select element using jQuery means programmatically deleting specific or all <option> elements within a <select> dropdown. This can be done using jQuery methods like remove() or empty(), allowing dynamic updates of the dropdown's available choices based on conditio
    3 min read
  • How to get N options from the <select> element using JQuery?
    The task is to get the random N options from the select element. Below are few of the approaches: Approach 1: First get the all option element's text in the array. Generate a random index of the array and get the option of that index. Swap the last element with the current random index and subtract
    3 min read
  • How to create Selected Option Select using jQuery Mobile ?
    jQuery Mobile is a web-based technology used to make responsive content that can be accessed on all smartphones, tablets, and desktops. In this article, we will be creating a Selected Option Select using jQuery Mobile. Approach: First, add jQuery Mobile scripts needed for your project. <link rel=
    1 min read
  • How to add options to a select element using jQuery?
    We will dynamically add the options to the select element using jQuery. jQuery has a lot of in-built methods. But, we will use some of them to accomplish the purpose of dynamically adding the options to the select element as listed below: Table of Content By passing the HTML of the option tag to the
    4 min read
  • How to create left positioned icon selects using jQuery Mobile ?
    jQuery Mobile is a web-based technology used to make responsive content that can be accessed on all smartphones, tablets and desktops. In this article, we will be making left positioned icon selects using jQuery Mobile. Approach: First, add jQuery Mobile scripts needed for your project. <link rel
    1 min read
  • How to create top positioned icon selects using jQuery Mobile ?
    jQuery Mobile is a web-based technology used to make responsive content that can be accessed on all smartphones, tablets and desktops. In this article, we will be making top positioned icon selects using jQuery Mobile. Approach: First, add jQuery Mobile scripts needed for your project. <link rel=
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences