422
422

Reputation: 5770

jquery error undefined function

I created this fiddle from code found on Stack, but it is firing an error. Can you help please.

My Fiddle: http://jsfiddle.net/422steve/DVNGc/

Original anser : https://stackoverflow.com/a/13000179/501173

Error I am getting is: TypeError: $allExtended.slideUp() is not a function

js:

$('.holder').on('click','a',function(e){   
    e.preventDefault();
    var $allExtended = $(this).closest('.wrapperdoo').find('.extended'),
        $extended = $allExtended.eq( $(this).index() ),        
        doIt= ($extended.is(':visible')) ? $extended.slideUp() : ($allExtended.slideUp()) ($extended.slideDown());   
});

Upvotes: 0

Views: 484

Answers (2)

Joseph at SwiftOtter
Joseph at SwiftOtter

Reputation: 4321

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'); });

Upvotes: 2

SLaks
SLaks

Reputation: 887275

($allextended.slideUp()) ($extended.slideDown()) is very wrong.
The code you wrote calls slideUp(), then calls the return value of slideUp as a function, passing slideDown().

It looks like you're trying to run two statements; to do that, you need an if block.

Upvotes: 1

Related Questions