View Single Post
Old 12-01-2012, 03:02 PM   PM User | #14
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 849
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
How can I get all the product images src attributes and store them into an array?

I tried this:
Code:
var imgDetails = new Array();

$(function(){
	imgDetails = $('.prod img').attr('src');
});
console.log(imgDetails);
and this:
Code:
$(function(){
	for(var i=0;i<40;i++){
		imgDetails[i] = $('.prod img').attr('src');
		alert(imgDetails);
	}
});
console.log(imgDetails);
I think I need a loop but I'm not sure how to get the length of how many images they are. I know the length but didn't want to hardcode it incase I add more. I am trying to target all the images which comes from my database. (when they are retrieved, I give them a class of 'pos') I thought doing this I could target them with jquery using the class I'm just not sure on the functions of jquery and how to retrieve all the images. Using the second code everytime I click the alert box off, it pops up with another but displays this:
Code:
core/product_files/bench1.jpg,core/product_files/bench1.jpg,core/product_files/bench1.jpg,core/product_files/bench1.jpg,core/product_files/bench1.jpg
i know to use the .attr('src') to get the value but how do I initially get the images and loop the src into the array?

Can I give them any naming convention as a way to target them for example:

Code:
<img name="0" />
<img name="1" />
<img name="2" />
<img name="3" />
<img name="nth" />
Then when I use the loop I can also grab that value and use the i variable to iterate through them?
Any hints you can give me?

EDIT: Awesome, just looked at the each() function on the jquery api and it's alerting me the src values.

Regards,

LC.

Last edited by LearningCoder; 12-01-2012 at 03:10 PM..
LearningCoder is offline   Reply With Quote