3 Replies - 7741 Views - Last Post: 18 May 2011 - 05:46 PM

#1 cRaZi_RiCaN   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 25-May 07

Populating an array.

Posted 17 May 2011 - 07:10 PM

Greetings. I am trying to populate an array with some image paths. I keep getting the error "Object ,, has no method [function]"

JS:

var imageSources = new Array(3);

jQuery.fn.setImages = function()
{
	//pre-load images
	for( var i = 0; i < this.length ; i++)
	{
		this[i] = "image" + i + ".png";
	}
};

imageSources.setImages();



What am I doing wrong?

Is This A Good Question/Topic? 0
  • +

Replies To: Populating an array.

#2 japanir   User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Populating an array.

Posted 18 May 2011 - 02:36 AM

Is there a reason you try to do it with JQuery? Javascript is much more suitable here.
var imageSources = new Array(3);
function fill_arr(){
  for(var i = 0; i < imageSources.length; i++){
    imageSources[i] = "image" + i + ".png";
  }
}


To invoke a JQuery function you do:
$(element).function()


But perhaps I am wrong here, what are you trying to do with that code? is that just practice? or is it part of full code? what does that full code should do then?
Was This Post Helpful? 0
  • +
  • -

#3 japanir   User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Populating an array.

Posted 18 May 2011 - 02:49 AM

Just to expand on why you get that error, imageSources is an array, and as such doesn't have the setImages method.
to make that method available, use:
function ImageSources() {
   this.imgs = new array(3);
   this.setImages = function(){
     for(var i = 0; i < this.imgs.length; i++){
       this.imgs[i] = "image" + i + ".png";
     }
   };
   return true;
}

var im = new ImageSources();
im.setImages();



Again, that is only Javascript
Was This Post Helpful? 1
  • +
  • -

#4 cRaZi_RiCaN   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 25-May 07

Re: Populating an array.

Posted 18 May 2011 - 05:46 PM

It seems I did not understand the concept of a library of X programming language. I thought I could just substitute all of my JS code with jQuery. I understand how it works now. Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1