Tutorialspoint.dev

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:

<!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>                    

Output:
SS1

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



<!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>
        <script type="text/javascript">
            function datalistcall(){
                var o1 = document.getElementById("carsInput").value;
                document.getElementById("output").innerHTML = "You select "
                    + o1 + " option";
            }
        </script>
    </body>
</html>                    

Output:
SS2

Supported Browsers: The browsers which support aside tag are listed below:

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

Note: Safari browser is not supported this tag.



This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter