Home >Web Front-end >CSS Tutorial >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>
.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; }
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'); };
Explanation:
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!