1

I am trying to call a user defined function within a jquery function to no avail I have tried this:

function close_quote_bar() {
    alert('oh hai');
}


if($('#sliding_quote:visible')) {

    $('#sliding_quote').slideUp();
    $('#trans_div').animate({opacity: 0.0}).css('display','none').close_quote_bar;

}

and this

//Function to close quote bar
function close_quote_bar() {
    alert('oh hai');
}

$('#get_quote_bar img').click(function() {                          
    if($('#sliding_quote:visible')) {               
        $('#sliding_quote').slideUp();
        $('#trans_div').animate({opacity: 0.0}).css('display','none').close_quote_bar;

    }               
});

With not much luck! Nothing happens when I cal close_quote_bar, or I get an Object is missing method error!

hope you guys can point me in the right direction I am really struggling with this one

3
  • You need to call your custom function in the callback of the animate or after you've set the css. It doesn't work at the moment as your function is not a method of the jquery object. Commented Feb 6, 2013 at 23:29
  • Neither are you calling it, nor is it a method of the jQuery object. Also, when do you want to get the alert? Commented Feb 6, 2013 at 23:30
  • 2
    Btw: I guess you want if ($('#sliding_quote').is(':visible')), currently the jQuery wrapper is always truthy (even if it contains nothing). Commented Feb 6, 2013 at 23:32

5 Answers 5

2

I assume you want something like this:

$('#sliding_quote:visible').slideUp( function(){
    $('#trans_div').css('display','none');
    close_quote_bar();
} );

It uses the slide up callback to then set the object to display none and run your function. This code also does away with the if statement by instead using the selector to only affect the visible element.

Sign up to request clarification or add additional context in comments.

5 Comments

looks like OP wants his $('#trans_div') to fade out. Perhaps use .fadeOut() instead of .css(...)
Fair point, maybe do: $('#trans_div').fadeOut(function(){close_quote_bar();});
yup, realized that as I was editing my comment, and decided to add it as my own answer ;-)
this looks great, i'll give it a go now, thanks Laurence :P However, could someone give me an idea why my current code doesn't work, also I am aware that it should have brackets after the functional call -I was trying a few things and left them out when I pasted the code
to execute a function, you need the (). Also, it takes a bit more code to make a function a jquery "chainable" method ( $(something).another().somethingElse()...). Generally you just exectute functions as funcName(); on their own line
0

you should try this:

$('#trans_div').animate({opacity: 0.0}, 400, function(){
    $(this).css('display','none');
    close_quote_bar();
});

Comments

0

Building on @laurencek's answer.

$('#sliding_quote:visible').slideUp( function(){
    $('#trans_div').fadeOut( function(){
      close_quote_bar();
    });
});

this executes close_quote_bar() as a callback of fadeOut.

2 Comments

No, it executes an unnecessary anonymous function as the callback. Why not $('#trans_div').fadeOut(close_quote_bar);?
you're right of course ;-). I always use the anonymous function pattern though. I do this mostly out of habit, but IMO it makes it easier to update and read. Guesse I shoulda said "in the callback of"
0

This line of your code needs to be changed

$('#trans_div').animate({opacity: 0.0}).css('display','none').close_quote_bar;

You have to call it like this

$('#trans_div').animate({opacity: 0.0}).css('display','none');
close_quote_bar();

And finally to trigger your closequotebar AFTER your animation have finished to animate, use this code, notice that a function() is in the 3rd parameter.

$('#trans_div').animate({opacity: 0.0}, 400, function(){
    $(this).css('display','none');
    close_quote_bar();
});

both animate and slideUp supports adding function() block as their parameters, so you could call a function after the animating has finished http://api.jquery.com/animate/ see the "complete" part

Comments

0

It turns out the function was being called and it was not an issue with the scope or anything like that.

I had another function that was executing on scroll (.scroll) that was conflicting with the function hence why it was behaving unexpectedly and appeared not to be executing.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.