Open In App

How to Add Drop Down List in Table Cell ?

Last Updated : 14 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Drop-downs are the user interface elements. Drop Down List is used to select one out of various options. This article focuses on creating a drop down list in the table cell which helps in selecting the choices directly from the table itself.

Below are the approaches to add a Drop Down List in a Table Cell:

Using HTML only

In this approach, a drop-down list will be created using only in-built HTML tags. It will be a static dropdown list where options can be added only during the creation of the drop-down.

  • select tag used to create the drop down.
  • option tag used to create choices inside the drop down.

Example: This example describes the implementation of a drop down list using only HTML.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <title>
          Drop Down List in Table Cell Using HTML/CSS
      </title>
    <link rel="stylesheet" href="style.css">
</head>

<body>
    <table>
        <tr>
            <th>Header Text 1</th>
            <th>Header Text 2</th>
        </tr>
        <tr>
            <td>
                <select>
                    <option value="optionText1">
                          Option Text 1
                      </option>
                    <option value="optionText2">
                          Option Text 2
                      </option>
                    <option value="optionText3">
                          Option Text 3
                      </option>
                </select>
            </td>
            <td>Cell Text</td>
        </tr>
    </table>
</body>

</html>
CSS
/* style.css*/

table {
    width: 80vh;
}

th, td {
    border: 1px solid #ddd;
    padding: 8px;
    text-align: center;
    width: 35vh;
}

tr{
    background-color: cadetblue;
    color: white;
    text-transform: uppercase;
}

select {
    padding: 8px;
    border: 1px solid #ccc;
    border-radius: 4px;
    background-color: #f1f1f1;
    font-size: 16px;
    cursor: pointer;
    outline: none;
    width: 35vh;
    text-align: center;
}

select:hover {
    background-color: #e0e0e0;
}

select:focus {
    border-color: #214974;
    box-shadow: 0 0 0 0.1rem rgba(0, 123, 255, 0.25);
}

Output:

outputHTMLCSS

OUTPUT FOR DROPDOWN CREATED INSIDE A TABLE CELL USING HTML/CSS

Using JavaScript

In this approach, a drop down list will be created using JavaScript methods.

  • document.getElementById() method is used to target the table cell in which drop down is to be created.
  • document.createElement() method is used to create select and option HTML Elements.
  • appendChild() – to dynamically add options in the select drop down.

Example: This example describes the implementation of a drop down list using JavaScript.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width, initial-scale=1.0">
    <title>Drop-down List in Table Cell</title>
    <link rel="stylesheet" href="style.css">
</head>

<body>
    <table>
        <tr>
            <th>Header Text 1</th>
            <th>Header Text 2</th>
        </tr>
        <tr>
            <td id="tableCell"></td>
            <td>Cell Text</td>
        </tr>
    </table>

    <script src="script.js"></script>
</body>

</html>
CSS
/* style.css*/

table {
    width: 80vh;
}

th, td {
    border: 1px solid #ddd;
    padding: 8px;
    text-align: center;
    width: 35vh;
}

tr{
    background-color: cadetblue;
    color: white;
    text-transform: uppercase;
}

select {
    padding: 8px;
    border: 1px solid #ccc;
    border-radius: 4px;
    background-color: #f1f1f1;
    font-size: 16px;
    cursor: pointer;
    outline: none;
    width: 35vh;
    text-align: center;
}

select:hover {
    background-color: #e0e0e0;
}

select:focus {
    border-color: #214974;
    box-shadow: 0 0 0 0.1rem rgba(0, 123, 255, 0.25);
}
Javascript
// script.js

const select = 
    document.createElement("select");
const options = [
    "Option Text 1", 
    "Option Text 2", 
    "Option Text 3"
];

options.forEach((option) => {
    const selectOptions = 
        document.createElement("option");
    selectOptions.value = 
        option.toLowerCase();
    selectOptions.textContent = option;
    select.appendChild(selectOptions);
});

const cell = 
    document.getElementById("tableCell");
cell.appendChild(select);

Output:

outputJavascript

OUTPUT FOR DROPDOWN CREATED INSIDE A TABLE CELL USING JAVASCRIPT



Similar Reads

How to add options to a drop-down list using jQuery?
Given an HTML document with a drop-down list menu and our task is to add more options to the drop-down list using jQuery. Approach : To add more option to the existing drop-down list using the append() method : var options = { val1: 'C#', val2: 'PHP' }; var selectOption = $('#programmingLanguage'); $.each(options, function (val, text) { selectOptio
2 min read
What are Cell Padding & Cell Spacing in HTML Table ?
In this article, we will discuss the cellpadding and cellspacing properties in HTML. These properties are used with the table tag for defining the spacing and padding of a table. Cellpadding: This property specifies the space between the border of a table cell and its contents (i.e) it defines the whitespace between the cell edge and the content of
2 min read
How to select first element in the drop-down list using jQuery ?
Given a drop-down list containing options elements, the task is to get the first element of the select element using JQuery. Approach 1: Select the first element of &lt;select&gt; element using the jQuery selector.Use the prop() method to get access to properties of that particular element.Change the selectedIndex property to 0 to get access to the
2 min read
How to generate a drop down list of timezone using PHP ?
The timezone_identifiers_list() function is used to generate a dropdown list of timezone with PHP. This function is used to return an indexed array containing all the timezone identifiers. The datetimezone object is sent as a parameter to the timezone_identifiers_list() function and it returns an indexed array on success or False on failure. This f
3 min read
How to create a group of related options in a drop-down list using HTML ?
We can define a group of related options in a drop-down list by using the &lt;optgroup&gt; Tag tag is used to create a group of same category options in a drop-down list. The tag is required when there is a long list of items. Example: &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt; How to define a group of related options in a drop-d
1 min read
How to change font style using drop-down list in JavaScript ?
To change or set a font style for certain text, the fontFamily CSS property needs to be changed. The fontFamily property sets or returns a list of font-family names for text in an element. Syntax: object.style.fontFamily = "font" To change the font style by option dropdown: The font values can be passed in option tags using option value. Syntax:
2 min read
How to define a drop-down list in HTML5 ?
In this article, we define a Drop-Down List By using the &lt;select&gt; Element in the Document. This tag is used to create a drop-down list. The &lt;select&gt; tag contains &lt;option&gt; tag to display the available option of drop-down list.Note: The tag is used in a form to receive user response.Syntax: &lt;select&gt; &lt;option&gt; &lt;option
1 min read
How to set the focus on drop-down list automatically when the page loads in HTML5?
The purpose of this article is to focus the drop-down list automatically when the page loads in HTML5. The &lt;select&gt; autofocus attribute is a boolean attribute that specifies that the drop-down list should automatically get focus when the page loads. Syntax: &lt;select autofocus&gt; Example: The following code demonstrates a drop-down list wit
1 min read
How to define an option in a drop-down list in HTML5 ?
In this article, we will learn how to define an option in a drop-down list in HTML5. The &lt;select&gt; element is used to create a drop-down lists in HTML. This is generally used to present a list of options to the user in a form so that the user could choose one option as the needed one. The &lt;option&gt; element which is defined inside the &lt;
2 min read
How to specify the name for drop-down list in HTML5 ?
Drop-down lists are also another way to allow people to choose only one choice from a number of alternatives. Since the default option is usually the first item on the list, using the drop-down list can be a safe choice if you know that one option is always preferred over the others. The drop-down list can only be used when the user must pick betwe
4 min read