I have a page with a bunch of fake dropdown menus that are coded like this (I can’t really change the HTML):
<div class="select">
<div class="wrapper calcdropdown">
<a href="#" class="dd-openercalc">Select a calculator</a>
<a href="#" class="dd-opener pulldown-arrow"> </a>
</div>
<ul class="selectbox">
<li>Dropdown item 1</li>
<li>Dropdown item 2</li>
<li>Dropdown item 3</li>
<ul>
</div>
When I click on either of the links inside the calcdropdown DIVs I need to toggle the corresponding selectbox UL. When I use toggle() the selectbox UL appears when I first click the links but doesn’t disappear when I click the links again.
var $j = jQuery.noConflict();
$j(document).ready(function() {
// hide all the dropdowns by default
$j(".selectbox").hide();
$j('.calcdropdown a').live('click',function(e) {
// hide the dropdowns in case another one's open
$j(".selectbox").hide();
var self = $j(this);
// show the selectbox for this link
var thisSelectbox = $j(this).parents('.select').children('.selectbox').toggle();
e.preventDefault();
});
});
This doesn’t work, either:
var $j = jQuery.noConflict();
$j(document).ready(function() {
$j(".selectbox").hide();
$j('.calcdropdown a').live('click',function(e) {
$j(".selectbox").hide();
var self = $j(this);
var thisSelectbox = $j(this).parents('.select').children('.selectbox');
if(thisSelectbox.is(':hidden')) {
thisSelectbox.show();
} else {
thisSelectbox.hide();
}
});
});
How can I hide the selectboxes after I show them?
Remove
$j(".selectbox").hide();from the click-function.Otherwise you always do this steps inside the click-function:
Edit:(I didn’t realize that you use more than one of those elements)
To hide all .selectbox except the one related to the click use not():
http://jsfiddle.net/doktormolle/qG8ps/