using an if statement on an object will always return true. .next() returns a jquery object. even if the object contains no elements, it is still an object and will always return true, so you always return out of the function early. try this:
- $(function() {
- var clicker = $('#test-list').find('dt');
- $(clicker).bind('click', function() {
- if ( $(this).next('dd:visible')[0] ) {
- return;
- }
- else {
- $('dd:visible').slideUp('slow');
- $(this).next('dd').slideDown('slow')
- }
- });
- });
i added the bolded text.
Edit: added jsbin
Edit2: clarification. [0] returns the element in position 0 of the selected elements in the jquery object. if none are selected, it will return undefined which evaluates to false in an if statement, otherwise it will evaluate to true in an if statement. you can replace [0] with .length === 1 if you want it to be more readable
-- Kevin