0

I wanna pass an object from jquery using a selector to a javascript function like below.

flash($("#something span"), "#fff", 250);

but it does not seem to be working.

and I am not sure what to put in the javascript function. What I make now is:

function flash(obj,color1,color2,duration) {
    obj.animate({backgroundColor:color1},duration);
    setTimeout(function(){this.animate({backgroundColor:color2},duration);},duration);
}

or is there another way instead of passing an object? for example, in jquery: $("this").flash("#f79999", "#eee", 250);

but then how to define the javascript function?

1
  • 6
    jQuery is JavaScript Commented Oct 23, 2012 at 23:30

3 Answers 3

1

You have a simple syntax error.

ojb.animate({backgroundColor:color},duration);

Should be

obj.animate({backgroundColor:color},duration);
Sign up to request clarification or add additional context in comments.

1 Comment

Also, note that JQuery is a framework for javascript, so it is javascript.
0

You'll have to include the jQuery.Color plugin to animate properties like background-color.

From "Animation Properties and Values:"

All animated properties should be animated to a single numeric value, except as noted below; most properties that are non-numeric cannot be animated using basic jQuery functionality (For example, width, height, or left can be animated but background-color cannot be, unless the jQuery.Color() plugin is used). [...]


is there another way instead of passing an object? for example, in jquery: $("this").flash("#fff",250);

jQuery's Plugins/Authoring should help with this. But, you could define it as:

jQuery.fn.flash = function (color, duration) {
    return this.animate({ backgroundColor: color }, duration);
};

$('#something span').flash('#fff', 250);

7 Comments

yes I did, but I don't know how to define function in javascript.
@KyleXie The definition in your question should work fine with jQuery.Color included. For the $(this).flash(...) variant, see my edit.
Thanks, it works. But there comes another issue. The function "setTimeout()" does not work inside the flash(). My flash method is to let the background to the a color, say red, and then turn back to the original color. flash() { this.animate({backgroundColor:to},duration); setTimeout(function(){this.animate({backgroundColor:from},250);},250); } but the now only the first line runs while the second line does not.
@KyleXie Keep using obj rather than this inside the setTimeout function. You can also use .delay() to chain animations: jsfiddle.net/UaY4j
@KyleXie It's an issue of context. Every function has its own value for this and you have 2 functions -- flash itself and the anonymous function passed to setTimeout. To specify the value of this for the anonymous function, you can to use bind or $.proxy.
|
0

With the jQuery.Color plugin installed in the page (ack. @Jonathan Lonowski) ...

You can avoid passing an object by using javascript's .call() or .apply().

Here's an example :

$(function() {
    function flash(color, duration) {
        this.animate({backgroundColor:color}, duration);
    }
    $("#something span").on('click', function() {
        flash.call($(this), "#fff", 250);
    });
    $("#something_else span").on('click', function() {
        flash.apply($(this), ["#fff", 250]);
    });
});

.call() and .apply() aren't really necessary here; regular function calls would suffice.

.call() and .apply() are normally used in cases where you need to call a method of one object but give it the context of another object. By doing so, the method is made to operate on a different this.

2 Comments

Thank you both. But the setTimeout() method does not work inside the flash method, have any idea?
There was no setTimeout, you have edited it in! The simplest solution is not to use setTimeout but to use jQuery's .delay(), which is a chainable method that can be inserted into an animation sequence, obj.animate(...).delay(...).animate(...);.

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.