HTML | list Attribute

The list attribute in HTML is used to identify a list of pre-defined options for an <input> element to suggest the user.

Usage: This attribute is used by the <input> element.

Syntax:



<input list="name">

Where, name is a string that will work as id and will be used to link the <input> element with the <datalist> element.

Below programs illustrate the list attribute:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html> 
<html
    <head
        <title>HTML list Attribute</title
    </head
      
    <body
        <h1 style = "color:green">HTML list Attribute</h1>
          
        <form action=""
            <label>Your Cars Name: </label
            <input list="cars"
            <datalist id="cars"
                <option value="BMW"/> 
                <option value="Bentley"/> 
                <option value="Mercedes"/> 
                <option value="Audi"/> 
                <option value="Volkswagen"/> 
            </datalist
        </form
    </body
</html>                    

chevron_right


Output:
list

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html> 
<html
    <head
        <title>Datalist Tag</title
    </head
      
    <body style = "text-align:center"
        <h1 style = "color:green">HTML list Attribute</h1>
  
        <form action=""
            <label>Your Cars Name: </label
            <input list="cars" id="carsInput"
            <datalist id="cars"
                <option value="BMW"/> 
                <option value="Bentley"/> 
                <option value="Mercedes"/> 
                <option value="Audi"/> 
                <option value="Volkswagen"/> 
            </datalist
              
            <button onclick="geek()" type="button">
                Click Here
            </button
        </form
          
        <p id="output"></p
          
        <script type="text/javascript"
            function geek(){ 
                var o1 = document.getElementById("carsInput").value; 
                  
                document.getElementById("output").innerHTML = "You select " 
                    + o1 + " option"; 
            
        </script
    </body
</html>                    

chevron_right


Output:
Before clicking the button:
list
After clicking the button:
list

Supported Browsers: The browser supported by list attribute are listed below:

  • Apple Safari Not supported
  • Google Chrome 20.0
  • Firefox 4.0
  • Opera 9.6
  • Internet Explorer 10.0


My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.