Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • 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
  • HTML Tutorial
  • HTML Exercises
  • HTML Tags
  • HTML Attributes
  • Global Attributes
  • Event Attributes
  • HTML Interview Questions
  • HTML DOM
  • DOM Audio/Video
  • HTML 5
  • HTML Examples
  • Color Picker
  • A to Z Guide
  • HTML Formatter
Open In App

HTML DOM Select Object

Last Updated : 03 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The Select object in HTML DOM is used to represent an HTML <select> element. It provides properties and methods to manipulate the <select> element and its associated <option> elements.

Syntax:

  • To create <select> element.
    document.createElement("SELECT")
  • To access <select> element.
    document.getElementById("mySelect")

Select Object Properties:

  • autofocus: It is used to set or return the drop-down list automatically get focused when the page loads.
  • disabled: It is used to set or return whether the drop-down list is disabled, or not.
  • form: It returns the reference to the form that contains the drop-down list.
  • length:It returns the number of <option> elements in a drop-down list.
  • multiple: It is used to set or return whether more than one option can be selected from the drop-down list.
  • name: It is used to set or return the value of the name attribute of a drop-down list.
  • selectedIndex: It is used to set or return the index of selected option in a drop-down list.
  • size: It is used to set or return the value of the size of a drop-down list.
  • type: It returns the type of form element a drop-down list.
  • value:It is used to set or return the value of selected option in a drop-down list.

Select Object Methods:

  • add(): It is used to add an option to a drop-down list.
  • checkValidity(): It is used to check the validity of a drop-down list.
  • remove(): It is used to remove an option from a drop-down list.

Select Object Collections:

  • options: It returns the collection of all the options in a drop-down list.

Example 1: This example create the <select> element by using document.createElement() method.

html
<!DOCTYPE html> <html>  <head>     <title>         HTML DOM Select Object     </title> </head>  <body>     <h2>         HTML DOM Select Object     </h2>      <p>         Click on button to create select         and option element     </p>      <button id="btn" onclick="myGeeks()">         Create Select Element     </button>      <!-- script to create select element -->     <script>         function myGeeks() {             let sel = document.createElement("Select");             sel.setAttribute("id", "MySelect");             document.body.appendChild(sel);              let opt = document.createElement("option");             opt.setAttribute("value", "Banana");             let nod = document.createTextNode("Banana");             opt.appendChild(nod);             document.getElementById("MySelect").appendChild(opt);              let opt1 = document.createElement("option");             opt1.setAttribute("value", "Apple");             let nod1 = document.createTextNode("Apple");             opt1.appendChild(nod1);             document.getElementById("MySelect").appendChild(opt1);              document.getElementById("btn").disabled=true;         }     </script> </body>  </html> 

Output:

createSelect
create select element

Example 2: This example access the <select> element by using getElementById() method.

html
<!DOCTYPE html> <html>  <head>     <title>         HTML DOM Select Object     </title> </head>  <body>     <h2>HTML DOM Select Object</h2>     <select id="GFG">         <option>JavaScript</option>         <option>HTML</option>         <option>CSS</option>         <option>TypeScript</option>     </select>      <p>         Click on button to get the number         of option elements in dropdown.     </p>      <button onclick="myGeeks()">         Button     </button>      <p id="test"></p>      <!-- script to count select element -->     <script>         function myGeeks() {             let len = document.getElementById("GFG").length;             document.getElementById("test").innerHTML = len;         }     </script> </body>  </html> 

Output:

accessSelect
access select element

Supported Browsers:

The browser supported by DOM Select Object are listed below:

  • Opera
  • Google Chrome
  • Firefox
  • Apple Safari

S

Shubrodeep Banerjee
Improve
Article Tags :
  • Web Technologies
  • HTML
  • HTML-DOM

Similar Reads

    HTML DOM InputEvent
    The input event is fired when the user changes an element, the value of an element or <textarea> element. DOM InputEvent occurs when an element in the HTML document gets input from the user. InputEvent property: data: Returns the inserted characters.dataTransfer: Returns an object containing i
    2 min read
    HTML | DOM Parameter Object
    The Parameter Object in HTML DOM is used to create and access the <param> element with in the object.Parameters for plugin embedded with an element is done by using the element. Syntax: It is used to access a <param> element.var x = document.getElementById("myParam");It is used to create
    2 min read
    HTML | DOM Link Object
    HTML DOM Link Object is used to access HTML <link> element.Syntax: To Access a HTML element: document.getElementById("myLink"); To Create a New HTML element: document.createElement("LINK"); Property Values: ValueDescriptioncharsetIt assigns the character encoding of the linked documentcrossOri
    2 min read
    HTML DOM Style quotes Property
    The Style quotes Property in HTML DOM is used to set/return the type of quotation marks for embedded quotations. This element can be accessed by using getElementById() method. Syntax: To get the property.object.style.quotesTo set the property.object.style.quotes = "none | string string string string
    2 min read
    HTML DOM KeyboardEvent getModifierState() Method
    The KeyboardEvent getModifierState() method in HTML DOM is used to return whether a specified modifier key is pressed, or activated. The KeyboardEvent getModifierState() method returns true if the specified key is pressed, otherwise it returns false. The list of key which is pressed then Modifier ke
    1 min read
    HTML | DOM KeyboardEvent location Property
    The KeyboardEvent location property is used for returning a number that indicates the location of a key on the keyboard or device. The KeyboardEvent location property can be used on the onkeydown and onkeyup events but not on onkeypress. The number returned by KeyboardEvent location property is repr
    2 min read
    HTML DOM Option label Property
    The HTML DOM Option label property is used to get or set the label attribute of an <option> element in a drop-down list (<select> element). The label property represents the text label associated with the option, which is shown in the drop-down list.Syntax: It is used to return the label
    2 min read
    HTML DOM | KeyboardEvent altKey Property
    The KeyboardEvent altKey property in HTML DOM is a read-only property and used to return the boolean value which indicates the alt key is pressed or not. It returns True if alt key is pressed otherwise return false. Syntax: event.altKey Below program illustrates the KeyboardEvent altkey property in
    1 min read
    HTML | DOM Form target Property
    The DOM Form Target property is used to set or return the value of the target attribute of the form.The Target attribute is used to specify whether the submitted result will open in the current window, a new tab or on a new frame. Syntax: It is used to return the target property. formObject.target I
    3 min read
    HTML | DOM KeyboardEvent shiftKey Property
    The KeyboardEvent shiftKey property in HTML DOM is a read-only property and used to return a Boolean value which indicates the SHIFT key is pressed or not. The KeyboardEvent shiftKey property returns true if the SHIFT key is pressed, otherwise returns false. Syntax: event.shiftKey Below program illu
    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