LATEST >>

Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » HTML-CSS-PHP-JavaScript / JavaScript Codes » Simple Custom Select Dropdown Example Using Pure JavaScript

Simple Custom Select Dropdown Example Using Pure JavaScript

Simple-Custom-Select-Dropdown-Example-Using-Pure-JavaScript
This Vanilla JS code snippet helps you to create a custom select dropdown. You can arrange your options in an unordered list and wrap it into a custom select wrapper. Then this snippet will style it like a select dropdown on which you can trigger custom events.

There are many code snippets available online or on many other blogs and websites, but everyone cannot optimize your blog or website, so you need some optimized code snippets. So now checkout out the code snippet for your blog and website that will give you all features for your desired code. Now grab the ready-to-use code and paste it where you want.

Features:

  1. Light Weight.
  2. Pure JavaScript.
  3. Cross Browser.
  4. No JQuery Files.
  5. Fully Customizable.
  6. Responsive.

How To Create Simple Custom Select Dropdown Example Using Pure JavaScript?

There are a few easy and understandable steps to achieve your desired functionality that we are gonna share below. Follow each step perfectly.

CSS:

<style type="text/css">
*,
*:after,
*:before {
box-sizing: border-box;
}
p{
margin: 20px 0;
}

.dropdown-wrapper {
position: relative;
width: 240px;
margin: 10px;
padding: 12px 15px;
background: #fff;
border-radius: 5px;
box-shadow: 0 1px 0 rgba(0, 0, 0, 0.2);
cursor: pointer;
outline: none;
transition: all 0.3s ease-out;
}
.dropdown-wrapper:after {
content: "";
width: 0;
height: 0;
position: absolute;
top: 50%;
right: 15px;
margin-top: -3px;
border-width: 6px 6px 0 6px;
border-style: solid;
border-color: #f05b55 transparent;
}
.dropdown-wrapper.is-active {
border-radius: 5px 5px 0 0;
background: #f05b55;
box-shadow: none;
border-bottom: none;
color: white;
}
.dropdown-wrapper.is-active:after {
border-color: #ffffff transparent;
transform: rotate(180deg);
}
.dropdown-wrapper.is-active .dropdown-list {
border-bottom: 1px solid rgba(0, 0, 0, 0.2);
max-height: 400px;
}

.dropdown-list {
/* Size & position */
position: absolute;
top: 100%;
left: 0;
right: 0;
/* Styles */
background: #fff;
border-radius: 0 0 5px 5px;
border: 1px solid rgba(0, 0, 0, 0.2);
border-top: none;
border-bottom: none;
list-style: none;
transition: all 0.3s ease-out;
/* Hiding */
max-height: 0;
overflow: hidden;
}
.dropdown-list li {
padding: 0 10px;
}
.dropdown-list li:hover a {
color: #f05b55;
}
.dropdown-list li:last-of-type a {
border: none;
}
.dropdown-list a {
display: block;
text-decoration: none;
color: #333;
padding: 10px 0;
transition: all 0.3s ease-out;
border-bottom: 1px solid #e6e8ea;
}
</style>

HTML:

<p> The following is the custom select dropdown:</p>
<div id="dropdown-wrapper" class="dropdown-wrapper" tabindex="1">
<span>Choose your User</span>

<ul class="dropdown-list">
<li><a href="#">Débora Correia</a></li>
<li><a href="#">Renato Oliveira</a></li>
<li><a href="#">Fernando Lins</a></li>
<li><a href="#">José Menezes</a></li>
</ul>
</div>

JavaScript:

<script type="text/javascript">
const dd = document.querySelector('#dropdown-wrapper');
const links = document.querySelectorAll('.dropdown-list a');
const span = document.querySelector('span');

dd.addEventListener('click', function() {
this.classList.toggle('is-active');
});

links.forEach((element) => {
element.addEventListener('click', function(evt) {
span.innerHTML = evt.currentTarget.textContent;
})
})
</script>

Customization:

No need to customize it. Just copy-paste. Rest edit the code as per comments and need.

Recommended For You:
How To Open/Show And Close/Hide DIV With CSS Code?

Troubleshooting the Errors:

Do it with concentration and patience. Check your all steps again and all codes or scripts. If you find any error you can contact us anytime via comment or better via email, We are always here to help you.

Final Words:

That’s all we have. We hope that you liked this article. If you have any problem with this code in your template then feel free to contact us with a full explanation of your problem. We will reply to you as time allows us If you have any doubts or problems please comment below. We are happy to help you! If you liked this article, Don’t forget to share this with your friends so they can also take benefit from it and leave.

You Like It, Please Share This Recipe With Your Friends Using...

Be the first to write a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *