HTML | <datalist> Tag

The <datalist> tag is used to provide autocomplete feature in the HTML files. It can be used with input tag so that users can easily fill the data in the forms using select the data.

Syntax:

<datalist> ... </datalist>

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <title>Datalist Tag</title>
    </head>
    <body>
        <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:
SS1

Example 2: The <datalist> tag object can be easily accessed by input attribute type.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <title>Datalist Tag</title>
    </head>
    <body>
        <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="datalistcall()" type="button">
                Click Here</button>
        </form>
        <p id="output"></p>
                <!-- Will display the selelct option -->
        <script type="text/javascript">
            function datalistcall(){
                  var o1 = document.getElementById("carsInput").value;
                  document.getElementById("output").innerHTML = "You select "
                    + o1 + " option";
            }
        </script>
    </body>
</html>                     

chevron_right


Output:
SS2

Supported Browsers: The browsers which support <datalist> tag are listed below:

  • Google Chrome 20.0 and above
  • Internet Explorer 10.0 and above
  • Firefox 4.0 and above
  • Opera 9.0 and above
  • Safari 11.1

Note: Safari browser is not supported this tag.



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 :


1


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