2

If I've multiple Image objects created like so:

var img0 = new Image();
var img1 = new Image();
var img2 = new Image();

What is the easiest and/or fastest way to wrap these into a jQuery object?

My current approach seems suboptimal:

var imgsJq = $(img0).add( $(img1) ).add( $(img2) );
2
  • 2
    That would actually be the most appropriate way to do that, using add(), so it's fine, but $(img0).add(img1).add(img2); does seem easier. Commented May 20, 2013 at 14:21
  • Just create an object literal and reference it's properties within your jQuery code. easier to read that way. Commented May 20, 2013 at 14:22

2 Answers 2

5
var imgsJq = $([img0, img1, img2]);
Sign up to request clarification or add additional context in comments.

2 Comments

This works, but is'nt much different from using add(), still +1.
It is cleaner and probably faster than add(). You can improve it if you put your images in an array instead of in 3 variables, but it does not change too much. var imgs = [new Image(), new Image(), new Image()], imgsJq = $(imgs);
1

yours' is fine perfectly but if you want you can use the following as it looks simpler

 var $img = $(img0).add(img1).add(img2);

or you can use this also

 var $img = $([img0,img1,img2]);

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.