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 values from a JSON object using jQuery ?
Next article icon

How to remove options from select element using jQuery ?

Last Updated : 24 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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 conditions.

Here we have some common approaches to to remove options from select element using jQuery:

Table of Content

  • Using the .remove() Method
  • Using the .empty() Method

Approach 1: Using the .remove() Method

The .remove() method in jQuery allows you to selectively remove <option> elements from a <select> element. By targeting specific options using their value or other selectors, .remove() deletes those options from the dropdown dynamically.

Example 1: In this example we use jQuery to remove an option from a dropdown when a button is clicked. It updates a message to notify that the option with value “val_1” is removed.

html
<!DOCTYPE html> <html lang="en">  <head>     <meta charset="UTF-8">     <meta name="viewport"            content="width=device-width,                     initial-scale=1.0">     <title>Remove Option from Select</title>     <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script> </head>  <body>      <h1 style="color:green;">GeeksForGeeks</h1>      <p id="GFG_UP"></p>      <select id="mySelect">         <option value="val_1">Val_1</option>         <option value="val_2">Val_2</option>         <option value="val_3">Val_3</option>         <option value="val_4">Val_4</option>     </select>      <br><br>      <button>Click here</button>      <p id="GFG_DOWN" style="color: green; font-size: 24px; font-weight: bold;"></p>      <script>         // Update the text above the dropdown         $('#GFG_UP').text('Click on the button to remove an option from select');          // Add event listener to the button         $('button').on('click', function () {             // Remove the option with value 'val_1'             $("option[value='val_1']").remove();              // Update the message after the option is removed             $('#GFG_DOWN').text('Option with val_1 is removed!');         });     </script>  </body>  </html> 

Output:

How to remove options from select element using jQuery ?

Using the .remove() Method to remove options from select element

Approach 2: Using the .empty() Method

The .empty() method in jQuery removes all child elements from a selected element, including all <option> elements in a <select>. This approach clears the entire dropdown, leaving the <select> element empty but still intact in the DOM.

Example: In this example we removes all options from a dropdown when the button is clicked using jQuery’s .empty() method. It updates a message notifying that all options have been removed.

HTML
<!DOCTYPE html> <html lang="en">  <head>     <meta charset="UTF-8">     <meta name="viewport"            content="width=device-width,                     initial-scale=1.0">     <title>Remove Option from Select</title>     <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script> </head>  <body>      <h1 style="color:green;">GeeksForGeeks</h1>      <p id="GFG_UP"></p>      <select id="mySelect">         <option value="val_1">Val_1</option>         <option value="val_2">Val_2</option>         <option value="val_3">Val_3</option>         <option value="val_4">Val_4</option>     </select>      <br><br>      <button>Click here</button>      <p id="GFG_DOWN"         style="color: green;                font-size: 24px;                font-weight: bold;">    </p>      <script>         // Update the text above the dropdown         $('#GFG_UP').text('Click on the button to remove all options from select');          // Add event listener to the button         $('button').on('click', function () {             // Use the .empty() method to remove all options from the select             $('#mySelect').empty();              // Update the message after all options are removed             $('#GFG_DOWN').text('All options have been removed!');         });     </script>  </body>  </html> 

Output:

How to remove options from select element using jQuery ?

Using the .empty() Method to remove options from select element



Next Article
How to select values from a JSON object using jQuery ?

P

PranchalKatiyar
Improve
Article Tags :
  • JavaScript
  • JQuery
  • Web Technologies
  • jQuery-Questions

Similar Reads

  • 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 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 remove all paragraphs from the DOM using jQuery ?
    To remove elements and content, we use jQuery remove() method which removes the selected element as well as everything inside it, also it will remove all bound events associated with that target element. Syntax: $(selector).remove() Example : C/C++ Code <!DOCTYPE html> <html> <head
    1 min read
  • How to select values from a JSON object using jQuery ?
    In this article, we will select values from a JSON object and display them on the browser using jQuery. To select values from a JSON object to webpage, we use append() method. This append() method in jQuery is used to insert some content at the end of the selected elements. Syntax: $(selector).appen
    2 min read
  • 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 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 remove contents of elements using jQuery ?
    In this article, we will discuss how to remove the contents of the elements using jQuery. To remove the contents of elements, we will use the empty() method and the remove() method. The jQuery empty() method is used to remove all child nodes and their content for the selected elements. This method d
    3 min read
  • How to remove table row from table using jQuery ?
    Removing a table row using jQuery involves selecting a specific row within an HTML table and deleting it dynamically. This can be done through various methods like .remove() or .detach(), allowing developers to manipulate the table structure efficiently without reloading the page. Here we have some
    3 min read
  • How to select first element in the drop-down list using jQuery ?
    Given a drop-down list containing options elements, the task is to get the first element of the select element using JQuery. Approach 1: Select the first element of <select> element using the jQuery selector.Use the prop() method to get access to properties of that particular element.Change th
    2 min read
  • How to select an element by name with jQuery ?
    Selecting an element by name with jQuery allows you to target HTML elements using their `name` attribute. This method is commonly used in form handling, where input fields, checkboxes, or radio buttons share the same name, enabling you to easily manipulate or retrieve their values. Table of Content
    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