0

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

3 Answers 3

2

Is there anything wrong with:

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

??

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

4 Comments

Not sure why but this isn't calling my function... I've updated my question so you can see my real code with your suggestion in there, any ideas why this isn't working :-s
OK, I've just realised why it's not working, it's because there wasn't a dollar sign in front of my first function test. However, putting a $ in front of it executes the code immediately when the page loads, and I don't want that. Any idea how to stop the code from firing until I want it to?
2 questions - does the test() function work on it own ( i.e. get rid of the timeline.stop code )? And does the timeline.stop code work process with the test() in there?
Got it, just needed to remove the round braces as per @deepak's suggestion
1

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

Comments

1

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

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

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.