I am looking for a solution to be able to display the elements that I select in my dropdown in the first value, that is, I have a dropdown with a list of values in the checkbox which I would like to display in the first section of my dropdown without having to display everything the list.
The following is an example of the dropdown with checkbox that I am implementing:
$(".checkbox-dropdown").click(function () {
$(this).toggleClass("is-active");
});
$(".checkbox-dropdown ul").click(function(e) {
e.stopPropagation();
});
.checkbox-dropdown {
width: 200px;
border: 1px solid #aaa;
padding: 10px;
position: relative;
margin: 0 auto;
user-select: none;
}
/* Display CSS arrow to the right of the dropdown text */
.checkbox-dropdown:after {
content:'';
height: 0;
position: absolute;
width: 0;
border: 6px solid transparent;
border-top-color: #000;
top: 50%;
right: 10px;
margin-top: -3px;
}
/* Reverse the CSS arrow when the dropdown is active */
.checkbox-dropdown.is-active:after {
border-bottom-color: #000;
border-top-color: #fff;
margin-top: -9px;
}
.checkbox-dropdown-list {
list-style: none;
margin: 0;
padding: 0;
position: absolute;
top: 100%; /* align the dropdown right below the dropdown text */
border: inherit;
border-top: none;
left: -1px; /* align the dropdown to the left */
right: -1px; /* align the dropdown to the right */
opacity: 0; /* hide the dropdown */
transition: opacity 0.4s ease-in-out;
height: 100px;
overflow: scroll;
overflow-x: hidden;
pointer-events: none; /* avoid mouse click events inside the dropdown */
}
.is-active .checkbox-dropdown-list {
opacity: 1; /* display the dropdown */
pointer-events: auto; /* make sure that the user still can select checkboxes */
}
.checkbox-dropdown-list li label {
display: block;
border-bottom: 1px solid silver;
padding: 10px;
transition: all 0.2s ease-out;
}
.checkbox-dropdown-list li label:hover {
background-color: #555;
color: white;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="checkbox-dropdown">
Seleccione una opción
<ul class="checkbox-dropdown-list">
<li>
<label>
<input type="checkbox" value="Vejle" name="city" />Milwaukee</label>
</li>
<li>
<label>
<input type="checkbox" value="Horsens" name="city" />Denver</label>
</li>
<li>
<label>
<input type="checkbox" value="Kolding" name="city" />Boston</label>
</li>
<li>
<label>
<input type="checkbox" value="Kolding" name="city" />LA</label>
</li>
</ul>
</div>
I explain it again, what I require is to display the selected elements of my dropdown in the first value, that is, they can be displayed where the value of "Seleccione una opción"
.
I hope someone can understand what I'm looking for.
You can have the selected items display in the space where it says "Select an option", but you need to create a new container for the message and the list of items that have been selected, displaying only one or the other.
Some additional CSS rules are also needed to know how to display the list of selected items.
Problem:
Solution: Add an event
onChange
in your type elementscheckbox
so that when they are selected, a process of collecting selected elements is executed and then dynamically substitute in the text that saysSeleccione una opción
It is necessary to occupy the
each()
jQuery manipulation attribute, create an array in javascript and occupy the manipulation attributes.html()