Preloading images and adding class

The Code Buccaneer picture The Code Buccaneer · Feb 10, 2012 · Viewed 7.2k times · Source

I am using the following code to preload my images:

function preload(sources)
{
  var images = [];
  for (i = 0, length = sources.length; i < length; ++i) {
    images[i] = new Image();
    images[i].src = sources[i];
  }
}

How can I add a class to the image object? I tried images[i].class = 'classname', but this doesn't do the trick. Any suggestions?

Thanks!

Answer

ShankarSangoli picture ShankarSangoli · Feb 10, 2012

Use className instead of class.

images[i].className  = "className";