Home > Web Front-end > CSS Tutorial > How to Create a Custom Multiple Selection Dropdown Checklist with Checkboxes in HTML, CSS, and JavaScript?

How to Create a Custom Multiple Selection Dropdown Checklist with Checkboxes in HTML, CSS, and JavaScript?

Susan Sarandon
Release: 2024-11-17 02:12:03
Original
266 people have browsed it

How to Create a Custom Multiple Selection Dropdown Checklist with Checkboxes in HTML, CSS, and JavaScript?

Creating Multiple Selection Dropdown Checklists

When creating a dropdown list, it's often desirable to allow users to select multiple options. Using HTML, this can be achieved by employing checkboxes within the dropdown options.

Conventional Dropdown with Checkboxes:

When adding checkboxes to a dropdown list without additional code, the checkboxes appear in front of the dropdown field. To change this, a more advanced approach is required.

Implementing a Custom Dropdown Checklist:

The following code snippet demonstrates how to create a custom dropdown checklist:

<div>
Copy after login
.dropdown-check-list {
  display: inline-block;
}

.dropdown-check-list .anchor {
  position: relative;
  cursor: pointer;
  display: inline-block;
  padding: 5px 50px 5px 10px;
  border: 1px solid #ccc;
}

.dropdown-check-list .anchor:after {
  position: absolute;
  content: "";
  border-left: 2px solid black;
  border-top: 2px solid black;
  padding: 5px;
  right: 10px;
  top: 20%;
  -moz-transform: rotate(-135deg);
  -ms-transform: rotate(-135deg);
  -o-transform: rotate(-135deg);
  -webkit-transform: rotate(-135deg);
  transform: rotate(-135deg);
}

.dropdown-check-list .anchor:active:after {
  right: 8px;
  top: 21%;
}

.dropdown-check-list ul.items {
  padding: 2px;
  display: none;
  margin: 0;
  border: 1px solid #ccc;
  border-top: none;
}

.dropdown-check-list ul.items li {
  list-style: none;
}

.dropdown-check-list.visible .anchor {
  color: #0094ff;
}

.dropdown-check-list.visible .items {
  display: block;
}
Copy after login
var checkList = document.getElementById('list1');
checkList.getElementsByClassName('anchor')[0].onclick = function(evt) {
  if (checkList.classList.contains('visible'))
    checkList.classList.remove('visible');
  else
    checkList.classList.add('visible');
};
Copy after login

Explanation:

  • HTML: The HTML structure creates a basic dropdown with an anchor element representing the dropdown header and an unordered list containing the dropdown options with checkboxes.
  • CSS: The CSS styles define the layout and appearance of the dropdown, including the checkbox styling and the display behavior of the dropdown options.
  • JavaScript: The JavaScript code handles the opening and closing of the dropdown options when the anchor element is clicked.

The above is the detailed content of How to Create a Custom Multiple Selection Dropdown Checklist with Checkboxes in HTML, CSS, and JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template