5

What does $({}) in jQuery mean ? I saw this on victmo response on this question : Possible to fade out div border?

He used $({alpha:1}).animate({alpha:0}) how this is affecting the DOM and what happens literally?

3
  • 2
    It creates a jQuery object. In that case it will be an empty object. What he was doing was wrong. That code does not point to a DOM object. And you only animate DOM objects.
    – Ryan
    Sep 4, 2014 at 15:09
  • @true I wouldn't said he was wrong, it is a trick to animate properties that cannot be animated! Sep 4, 2014 at 15:22
  • @Karl-André Gagnon I agree it wasn't politically correct to say he was wrong.
    – Ryan
    Sep 4, 2014 at 15:41

1 Answer 1

6

What does $({ }) in jQuery mean ?

It means "pass an empty object to jQuery and create a jQuery object from it".

how this is affecting the DOM and what happens literally?

It doesn't affect the DOM at all, it simply changes the property value of the object {alpha:1} over time.

You can use some jQuery methods on plain objects as explained in the documentation. Although it seems to be a bit outdated since animate is not listed there. But it works indeed:

> $({alpha:1}).animate({alpha:0}, {step: function() { console.log(this.alpha); }})
1
0.9965342284774632
0.9870866934849247
0.9730426794137726
0.9524135262330098
0.9242551074907518
0.8926584654403724
0.8563192594626027
...
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.