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 elements using jQuery ?
Next article icon

How to detect if a dropdown list is a multi-select using jQuery?

Last Updated : 15 May, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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 dropdown defined in the following example which also has an attribute of multiple attached to it. Also, a button element with a class of check-multi-select is created which on click checks whether the given dropdown is a multi-select one or not. We use the prop() method to check the existence of the multiple attributes bypassing the same as a string parameter. If the method returns the boolean true, then the dropdown list is a multi-select. Otherwise, the dropdown list is not a multi-select since the method returns the boolean false which indicates the absence of the multiple attributes.

Example 1: In this example, we will use the above approach.

HTML
<!DOCTYPE html> <html> <head>     <style>         body {             text-align: center;         }         p {             font-size: 25px;             font-weight: bold;         }         select {             display: block;             margin: 0 auto;         }         button {             margin-top: 1rem;             cursor: pointer;         }     </style> </head> <body>     <h1 style="color: green">GeeksforGeeks</h1>     <p>jQuery - Detect if a dropdown is a multi-select</p>     <select id="dropdown" multiple>         <option value="Geek 1">Geek 1</option>         <option value="Geek 2">Geek 2</option>         <option value="Geek 3">Geek 3</option>         <option value="Geek 4">Geek 4</option>     </select>     <button class="check-multi-select">         Click me to check if dropdown is a multi-select     </button>     <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js">     </script>     <script type="text/javascript">         $(".check-multi-select").click(function () {             if ($("#dropdown").prop("multiple")) {                 alert(                   "The given dropdown IS a multi-select"                 );             } else {                 alert(                   "The given dropdown IS NOT a multi-select"                 );             }         });     </script> </body> </html> 

Output:

Example 2: This example is quite similar to the previous example but the only difference is that the dropdown is defined as not a multi-select which means there are no multiple attributes attached to it. Hence, the alert message states that the given dropdown list is not a multi-select.

HTML
<!DOCTYPE html> <html> <head>     <!-- Basic inline styling -->     <style>         body {             text-align: center;         }         p {             font-size: 25px;             font-weight: bold;         }         select {             display: block;             margin: 0 auto;         }         button {             margin-top: 5rem;             cursor: pointer;         }     </style> </head>  <body>     <h1 style="color: green">GeeksforGeeks</h1>     <p>jQuery - Detect if a dropdown is a multi-select</p>      <select id="dropdown">         <option value="Geek 1">Geek 1</option>         <option value="Geek 2">Geek 2</option>         <option value="Geek 3">Geek 3</option>         <option value="Geek 4">Geek 4</option>     </select>     <button class="check-multi-select">         Click me to check if dropdown is a multi-select     </button>     <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js">     </script>     <script type="text/javascript">         $(".check-multi-select").click(function () {             if ($("#dropdown").prop("multiple")) {                 alert(                   "The given dropdown IS a multi-select"                 );             } else {                 alert(                   "The given dropdown IS NOT a multi-select"                 );             }         });     </script> </body> </html> 

Output:


Next Article
How to select multiple elements using jQuery ?

R

rajatsandhu2001
Improve
Article Tags :
  • Web Technologies
  • JQuery
  • Geeks Premier League
  • Geeks-Premier-League-2022
  • CSS-Properties
  • HTML-Questions
  • jQuery-Questions

Similar Reads

  • How to select/deselect multiple options in dropdown using jQuery ?
    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 ta
    4 min read
  • How to add options to a drop-down list using jQuery?
    Given an HTML document with a drop-down list menu and our task is to add more options to the drop-down list using jQuery. Approach : To add more option to the existing drop-down list using the append() method : var options = { val1: 'C#', val2: 'PHP' }; var selectOption = $('#programmingLanguage');
    2 min read
  • How to change Selected Value of a Drop-Down List using jQuery?
    We will change the default selected value of the dropdown using jQuery. In an HTML select tag, the default selected value is the value given to the first option tag. With jQuery, it is easy to change the selected value from a drop-down list. Below are the methods to change the selected value of a dr
    3 min read
  • How to get selected text from a drop-down list using jQuery?
    In this article, we will learn how we can get the text of the selected option in a select tag using jQuery. There are two ways in jQuery that we can use to accomplish this task. Table of Content Using the val() method By using option:selected selector and text() method togehterUsing the val() method
    2 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 Create A Cascading Dropdown List Using JavaScript?
    The Cascading dropdown lists are used to display options in a secondary dropdown based on the selection made in a primary dropdown. This is useful in forms where users need to select categories or locations in the hierarchical manner. For example: selecting a country might reveal states or provinces
    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 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 an eye catchy Multiselect in jQuery ?
    In this article, we will create an eye catchy multi-select in jQuery. It is a selection menu in which we can select multiple values at the same time. For example, choosing different colleges. Step 1: First of all create an HTML file and add the following CDN links in the head tag. CDN link for jQuer
    3 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