Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

Responding to OP's question
Source Link

Here is what is going on - in your ternary operation, on the last line, you are trying to call two functions. That is not possible. You need to use an if block. Here is how I rewrote it. I don't know if you are using the doIt variable for any purpose, but you will want to check for undefined:

$('.holder').on('click','a',function(e){ 
 e.preventDefault();
 var $allextended = $(this).closest('.wrapperdoo').find('.extended'),
 $extended = $allextended.eq( $(this).index() ),
 doIt = undefined;
 if ($extended.is(":visible")) {
 doIt = $extended.slideUp(); 
 } else {
 $allextended.slideUp();
 doIt = $extended.slideDown();
 } 
});​

--- EDIT ---

To add the class to the href that is being clicked, just do it like this, in the function:

$(this).addClass('class');

If you want to remove the class when you are done, do it like this. First, assign the this variable as a global variable (so the this variables don't get mixed up), like so:

var sender = $(this);

Then, in your slideUp() or slideDown() functions, the second parameter is a callback for when it is completed:

$allextended.slideUp(500, function() { sender.removeClass('class'); });

Here is what is going on - in your ternary operation, on the last line, you are trying to call two functions. That is not possible. You need to use an if block. Here is how I rewrote it. I don't know if you are using the doIt variable for any purpose, but you will want to check for undefined:

$('.holder').on('click','a',function(e){ 
 e.preventDefault();
 var $allextended = $(this).closest('.wrapperdoo').find('.extended'),
 $extended = $allextended.eq( $(this).index() ),
 doIt = undefined;
 if ($extended.is(":visible")) {
 doIt = $extended.slideUp(); 
 } else {
 $allextended.slideUp();
 doIt = $extended.slideDown();
 } 
});​

Here is what is going on - in your ternary operation, on the last line, you are trying to call two functions. That is not possible. You need to use an if block. Here is how I rewrote it. I don't know if you are using the doIt variable for any purpose, but you will want to check for undefined:

$('.holder').on('click','a',function(e){ 
 e.preventDefault();
 var $allextended = $(this).closest('.wrapperdoo').find('.extended'),
 $extended = $allextended.eq( $(this).index() ),
 doIt = undefined;
 if ($extended.is(":visible")) {
 doIt = $extended.slideUp(); 
 } else {
 $allextended.slideUp();
 doIt = $extended.slideDown();
 } 
});​

--- EDIT ---

To add the class to the href that is being clicked, just do it like this, in the function:

$(this).addClass('class');

If you want to remove the class when you are done, do it like this. First, assign the this variable as a global variable (so the this variables don't get mixed up), like so:

var sender = $(this);

Then, in your slideUp() or slideDown() functions, the second parameter is a callback for when it is completed:

$allextended.slideUp(500, function() { sender.removeClass('class'); });
Source Link

Here is what is going on - in your ternary operation, on the last line, you are trying to call two functions. That is not possible. You need to use an if block. Here is how I rewrote it. I don't know if you are using the doIt variable for any purpose, but you will want to check for undefined:

$('.holder').on('click','a',function(e){ 
 e.preventDefault();
 var $allextended = $(this).closest('.wrapperdoo').find('.extended'),
 $extended = $allextended.eq( $(this).index() ),
 doIt = undefined;
 if ($extended.is(":visible")) {
 doIt = $extended.slideUp(); 
 } else {
 $allextended.slideUp();
 doIt = $extended.slideDown();
 } 
});​
lang-js

AltStyle によって変換されたページ (->オリジナル) /