SparrwHawk
SparrwHawk

Reputation: 14153

jQuery Call a Function in Addition to some other code

I'm trying to call a function into another function to get rid of some redundant code. The 'test' function is a standard piece of repetitive code, so I don't want to keep writing it out along with the custom animation.

(function test() {
    do this
});    

/* =========================Standard code========================= */
$(function () {
$('#div').click(EXECUTE THE 'TEST' FUNCTION HERE AND THEN...function() {
    $('#timeline').stop().animate({
        left : "+=500px"
    });
});

});

Here is the solution thanks to the kind guys below:

function test() {
    do this
}

/* =========================Standard code========================= */
$(function () {
$('#div').click(function() {
    test();
    $('#timeline').stop().animate({
        left : "+=500px"
    });
});

Upvotes: 0

Views: 82

Answers (3)

Deepak Kumar
Deepak Kumar

Reputation: 682

why you are using round braces around the test() function?

Can you try with an ALERT box in test() function?

Upvotes: 1

Schroedingers Cat
Schroedingers Cat

Reputation: 3139

Is there anything wrong with:

$(function () {
$('#div').click(function() {
    test();
    $('#timeline').stop().animate({
        left : "+=500px"
    });
});

??

Upvotes: 2

jerluc
jerluc

Reputation: 4316

You would put your call to test() above the $('#timeline').... line.

Upvotes: 1

Related Questions