Hussein
Hussein

Reputation: 42808

Jquery object caching

Is this the proper way to cache an object for later use.

var x = $value.children().andSelf().filter('embed'),

vsrc = x.attr('src'),
vwidth = x.attr('width'),
vheight = x.attr('height');

Upvotes: 0

Views: 1098

Answers (2)

Ben Blank
Ben Blank

Reputation: 56572

Yes, this is exactly how to "reuse" a jQuery object if you don't want to chain it. (Chaining can make your code hard to read if you overuse it, so this is often a good alternative.)

It's common, though not at all a standard, to give your variable a name starting with $ to mark it as a jQuery object:

var $embed = $value.children().andSelf().filter('embed');

(Joel on "apps hungarian" notation.)

Upvotes: 2

Šime Vidas
Šime Vidas

Reputation: 185883

Yes.

However, you could improve your formatting:

var x = $value.children().andSelf().filter('embed'),
    vsrc = x.attr('src'),
    vwidth = x.width(),
    vheight = x.height();

Also, you can use width() and height() to retrieve the dimensions of the element.

Upvotes: 3

Related Questions