
- HTML Tutorial
- HTML - Home
- HTML - Overview
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Formatting
- HTML - Phrase Tags
- HTML - Meta Tags
- HTML - Comments
- HTML - Images
- HTML - Tables
- HTML - Lists
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML - Frames
- HTML - Iframes
- HTML - Blocks
- HTML - Backgrounds
- HTML - Colors
- HTML - Fonts
- HTML - Forms
- HTML - Embed Multimedia
- HTML - Marquees
- HTML - Header
- HTML - Style Sheet
- HTML - Javascript
- HTML - Layouts
- HTML References
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Entities
- HTML - Fonts Ref
- HTML - Events Ref
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
HTML DOM Select add() Method
The HTML DOM select add() method adds a new option to a drop-down list in an HTML document.
Syntax
Following is the syntax −
object.add(option,index)
Example
Let us see an example of HTML DOM select add() method −
<!DOCTYPE html> <html> <head> <style> html{ height:100%; } body{ text-align:center; color:#fff; background: radial-gradient( circle farthest-corner at 23.1% 64.6%, rgba(129,125,254,1) 0%, rgba(111,167,254,1) 90% ) no-repeat; height:100%; } p{ font-weight:700; font-size:1.1rem; } .drop-down{ display:block; width:35%; border:2px solid #fff; font-weight:bold; padding:2px; margin:1rem auto; outline:none; } .btn{ background:orange; border:none; height:2rem; border-radius:2px; width:35%; margin:2rem auto; display:block; color:#fff; font-weight:bold; outline:none; cursor:pointer; } .show{ font-size:1.5rem; font-weight:bold; } </style> </head> <body> <h1>DOM Select add() method Demo</h1> <p>Hi, Select your favourite subject:</p> <select class='drop-down' name="Drop Down List"> <option>Physics</option> <option>Biology</option> </select> <button type="button" onclick="addSubject()" class="btn">Add History Subject</button> <div class="show"></div> <script> function addSubject() { var dropDown = document.querySelector(".drop-down"); document.querySelector(".show").innerHTML="Added History Subject"; var option=document.createElement("OPTION"); option.innerHTML="History"; dropDown.add(option); } </script> </body> </html>
Output
This will produce the following output −
Click on “Add History Subject” button to add history subject option in drop-down list.
- Related Articles
- HTML DOM Textarea select() Method
- HTML DOM Select remove() Method
- HTML DOM Select Object
- HTML DOM Select disabled Property
- HTML DOM Select length Property
- HTML DOM Select multiple Property
- HTML DOM Select name Property
- HTML DOM Select value Property
- HTML DOM Select type Property
- HTML DOM Select size Property
- HTML DOM Select form Property
- HTML DOM Select autofocus Property
- HTML DOM Select selectedIndex Property
- HTML DOM write() Method
- HTML DOM writeln() Method

Advertisements