I’m building a custom pulldown menu in jQuery.
Now I’d like to set the text of the A.select element to the text of the element that is clicked. But I can’t get it to work.
This is the html:
<div class="pulldown">
<a href="javascript:;" class="select">All cats/subs</a>
<div class="options">
<a href="javascript:;">Option one A</a>
<a href="javascript:;">Option two A</a>
<a href="javascript:;">Option three A</a>
<a href="javascript:;">Option four A</a>
</div>
</div>
This is the jQuery code:
$(document).ready(function() {
$('div.pulldown').toggle(function() {
$(this).css('z-index','110');
$('.options', this).css('z-index','100').show('fast');
$('.options A', this).click(function(){
var value = $(this).text();
$(this).closest('DIV.pulldown A.select').text(value);
});
}, function(){
$(this).css('z-index','10');
$('.options', this).css('z-index','0').hide('fast');
}); });
What am I missing here?
It will be easier to work with if you clean up your HTML a little bit:
Then your jQuery boils down to this:
A live demonstration can be found at http://jsfiddle.net/c8Qp8/.
Note, this example needs to be expanded to take care of multiple menus with multiple sub-items. Also, instead of setting your CSS properties with JavaScript you should set them with your stylesheet.