I am encountering an issue with the Jquery dropdown list click function. The addClass method is working properly, but the removeClass method is not functioning as expected. When I click on the dropdown list, it does not hide.
Here is a live demo: http://jsfiddle.net/wcbaxkdv/
Below is my code:
HTML:
<div id="dd" class="wrapper-dropdown-3" tabindex="-1">
<span>WHERE DO YOU WANT TO GO?</span>
<ul class="dropdown">
<li><a href="#">WHERE DO YOU WANT TO GO?</a></li>
<li><a href="#">addClass OK</a></li>
<li><a href="#">WHERE DO YOU WANT TO GO?</a></li>
<li><a href="#">removeClass Problem</a></li>
<li><a href="#">WHERE DO YOU WANT TO GO?</a></li>
<li><a href="#">WHERE DO YOU WANT TO GO?</a></li>
</ul>
</div>
CSS:
.wrapper-dropdown-3 {
background: none repeat scroll 0 0 #f0f1f1;
border: medium none;
border-radius: 0;
box-shadow: 0 1px 1px rgba(50, 50, 50, 0.1);
color: #8aa8bd;
cursor: pointer;
font-family: 'Archer-Book';
font-size: 13px;
font-weight: normal;
height: 45px;
margin-left: 25px;
margin-top: 25px;
outline: medium none;
padding-left: 10px;
padding-top: 15px;
position: relative;
vertical-align: middle;
width: 87%;
}
/* Additional CSS rules omitted for brevity */
Js:
function DropDown(el) {
this.dd = el;
this.placeholder = this.dd.children('span');
this.opts = this.dd.find('ul.dropdown > li');
this.val = '';
this.index = -1;
this.initEvents();
}
DropDown.prototype = {
initEvents : function() {
var obj = this;
obj.dd.on('click', function(event){
$(this).addClass('active');
return false;
});
obj.opts.on('click',function(){
var opt = $(this);
obj.val = opt.text();
obj.index = opt.index();
obj.placeholder.text(obj.val);
});
},
getValue : function() {
return this.val;
},
getIndex : function() {
return this.index;
}
}
$(function() {
var dd = new DropDown( $('#dd') );
$('.wrapper-dropdown-3 .dropdown li a').on('click', function() {
// all dropdowns
$('.wrapper-dropdown-3').removeClass('active');
});
});