views:

79

answers:

3

Firstly, Is there a way for me to put variable declarations in a for loop?

E.g. the following:

var origh1 = $('#candidates img:eq(0)').height();  
var origh2 = $('#candidates img:eq(1)').height();
var origh3 = $('#candidates img:eq(2)').height();
var origh4 = $('#candidates img:eq(3)').height();
var thumb1h = $('#candidates img:eq(0)').height()*0.20; 
var thumb2h = $('#candidates img:eq(1)').height()*0.20;
var thumb3h = $('#candidates img:eq(2)').height()*0.20;
var thumb4h = $('#candidates img:eq(3)').height()*0.20;

Then using that those variables, I would like the following in a for loop. Naturally, 'origh1' should equal img:eq(i), where i = 0. So I imagine if origh1 were in the loop, it would be something like origh(i+1).

$('#candidates img:eq(0)').fadeIn('normal').delay(300).animate({ height : origh1}, 1200, 'easeInQuad');
$('#candidates img:eq(1)').fadeIn('normal').delay(300).animate({ height : origh2}, 1200, 'easeInQuad');
$('#candidates img:eq(2)').fadeIn('normal').delay(300).animate({ height : origh3}, 1200, 'easeInQuad');
$('#candidates img:eq(3)').fadeIn('normal').delay(300).animate({ height : origh4}, 1200, 'easeInQuad');

Thanks :)

+2  A: 

For each will do the job:

$('#candidates img').each( function() {
 var origh = $(this).height();
 $(this).fadeIn('normal').delay(300).animate({ height : origh}, 1200, 'easeInQuad'); 
});

To iterate over first 4 change it to:

  $('#candidates img:lt(5)').each( function() {
     var origh = $(this).height();
     $(this).fadeIn('normal').delay(300).animate({ height : origh}, 1200, 'easeInQuad'); 
    });
Artem Barger
http://w3schools.com/jsref/jsref_obj_array.asp he will need to know this too.
Hrmm....the problem with this is that it doesn't seem to iterate through img:eq(0) to say img:eq(14). And subsequent origh1 - origh15.If I wanted to access origh5, how would I do that...based on this suggestion?
marcamillion
Correct, sorry, just copy-pasted this without additional thought.
Artem Barger
You can use :lt or :gt to define the range of your images.
Artem Barger
Can you update the code to reflect the use of :lt or :gt ? I understand the construct of what I want to do, it's just the syntax that bugs me out. Edit: I saw that you just changed it right after I submitted the comment. How do I access origh15 for example...based on that code. I see no allowance for incrementing origh, or am I missing something?Also, do you have documentation of :lt and :gt ? Where I can read up about what it does and how to use it?Thanks.
marcamillion
It's not enough that you understand what you want to do, you need us to understand this either.
Artem Barger
Huh? Sorry, don't fully understand your last comment.
marcamillion
I was saying you need to elaborate more, since to help you it's not enough to know what you understand what exactly you want to achieve.
Artem Barger
Oh...sorry about that. LOL @ the link to the let me google that for you. I actually Google'd it, but I was googling 'L't...not 'I't.Thanks for the update though :)Edit: Hrmm...just re-read it...it is 'L't, i.e. for less than. Interesting. I am having one of those days :(
marcamillion
You always welcome. There is jQuery API site, you can find there everything you need.
Artem Barger
Hey Artem...I just tried this...and it doesn't work. I was looking at the syntax and I thought it would work - which is why I chose it as the answer.I think the problem lies in the img:lt(5). I even tried img:eq(lt(5)) and that doesn't work. I don't think it saves the values in the variables correctly.
marcamillion
Please elaborate more, what do you want to achieve and what do you get instead.
Artem Barger
Edit your post with new code sample you trying to apply.
Artem Barger
A: 

You could put the results in an array and then do a for loop, populating the array...I think you would need 2 arrays, one for origh and thumbh.

hvgotcodes
Have an example of actual code? I am not fully familiar with the syntax of jQuery - just learning :)
marcamillion
A: 

Am I missing something? Or did you only want the first 4 img's?

$('#candidates img').fadeIn('normal').delay(300).animate({ height : $(this).height}, 1200, 'easeInQuad');
dotjoe
I want any number of images. I just used eq(0 - 3) as an example.Assume I want the first 20 or 30 images. There should be no limit.I would much rather change a for loop, and have it do it multiple times, than me having to re-write one line of code (manually) 20 times for 20 images.
marcamillion
oh good, this selector will get you all the imgs.
dotjoe
But this doesn't do anything for the variables.
marcamillion
You do not need them.
Artem Barger