Saturday, 13 August 2016

HTML Custom Dropdown using jQuery

HTML provides <select> tag to select value using its default dropdown. But some times there is a need for Custom dropdown. So in this tutorial we are going to build custom dropdown.

<!DOCTYPE html>
<html>
<head>
 <title>Custom Drop Down</title>
 <style>
 .dropdown {
    display: inline-block;
    margin: 10px;
    position: relative;
    padding: 0;
    border: 0;
    border-radius: 5px;
    font-size: 24px;
    font-weight: 400;
    background: #26A69A;
    color: #FFF;
    box-shadow: 0px 8px 17px 0px rgba(0, 0, 0, 0.2), 0px 6px 20px 0px rgba(0, 0, 0, 0.19);
    width:120px;
}
.dropdown > input {
    display: none;
    position: fixed;
    z-index: -2;
}
.dropdown > label {
    display: inline-block;
    max-width: 100%;
    margin-bottom: 5px;
    font-weight: 700;
}
.drop-ttl {
    font-weight: 400;
    padding: 5px 20px;
}
.dropdown ul {
    width: 100%;
    font-weight: 400;
    margin: 0;
    padding: 0;
    position: absolute;
    top: 0;
    text-align: center;
    list-style-type: none;
    background: #FFF;
    color: #26A69A;
    box-shadow: 0px 8px 17px 0px rgba(0, 0, 0, 0.2), 0px 6px 20px 0px rgba(0, 0, 0, 0.19);
    border-radius: 5px;
}
.dropdown ul >li {
    padding: 5px 20px;
    cursor: pointer;
}
.dropdown ul > li > a {
    color: inherit;
    outline: 0;
    text-decoration: none;
    cursor: pointer;
}
.dropdown ul > li:hover{
    background:#DEDEDE;
}
input[type="checkbox"]:checked ~ label > ul {
    display:none;
}

 </style>
</head>
<body>
<div class="dropdown">
    <input id="checkbox1" type="checkbox" name="checkbox" checked="">
    <label for="checkbox1">
        <div class="drop-ttl">Local</div>
        <ul class="dcf">
            <li><a>Local</a></li>
            <li><a>Regional</a></li>
            <li><a>National</a></li>
         </ul>
    </label>
</div>

 <script   src="https://code.jquery.com/jquery-2.2.4.js"  ></script>
 <script>
 $(function(){
    $('.dcf').on("click","li a",function(){
      $('.drop-ttl').html($(this).html());
    })
  });
</script>
</body>
</html>

No comments:

Post a Comment