• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 219
  • Last Modified:

what does this mean

var numberOfPics = $(this).find('img').length;
 
 var next = Math.floor(Math.random() * numberOfPics);


$(this).scrollTo(
 
 '#photos_inner>img:eq(' + next + ')',
 
 {duration: 1000}
 
 );

 
this piece of jquery code uses the scrollTo plugin, my question is what does ' + next + ' mean here?

why not just use   next ?
0
charmingduck
Asked:
charmingduck
  • 5
  • 3
  • 3
  • +2
4 Solutions
 
nap0leonCommented:
The single quotes indicate a change from "text" to "variables".
You use the "+" to say "concatenate".

Like, if I wanted to spell "LOL" I would do it like:
'L' + 'O' + 'L' = LOL

0
 
sonawanekiranCommented:
// Get total number of images on web page
var numberOfPics = $(this).find('img').length;
 
// Gets the number between 0 & numberOfPics.
// If numberOfPics is 10 then next is in between 0 and 10
 var next = Math.floor(Math.random() * numberOfPics);

//Easily scroll overflowed elements (which is your image)
// This will scroll to image level.
// Suppose next = 5 then it will scroll to image5
$(this).scrollTo(
 
 '#photos_inner>img:eq(' + next + ')',
 
 {duration: 1000}
 
 );
0
 
Gurvinder Pal SinghCommented:
It is simply going to scroll to the image whose index is randomly selected
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
charmingduckAuthor Commented:
yeah, i know what the code does, but

why do I have to use this
 '#photos_inner>img:eq(' + next + ')',


instead of this
 '#photos_inner>img:eq(next)',

oh, the latter won't work, why is that? if next is a variable, why can't it loose the single quotes? I dont understand  +next+  here
0
 
sonawanekiranCommented:
eq() needs string not integer
0
 
sonawanekiranCommented:
Sorry My mistake
0
 
charmingduckAuthor Commented:
http://api.jquery.com/eq/

.eq( index )
indexAn integer indicating the 0-based position of the element.


so eq() needs integer, right? next is integer already, right? so what's with the + ???
0
 
sonawanekiranCommented:
You are true.
Jquery works with selector which needs to be string. but eq needs integer so we need break it to assign integer value to eq function

"#selector img:eq(" + integervalue+ ")"

Like

'#photos_inner>img:eq(' + next + ')'
0
 
sonawanekiranCommented:
And if you use

'#photos_inner>img:eq(next)'

the variable next is not evaluted
0
 
tim_csCommented:
It's building a string for scrollTo() , see the first single quote after the second ( right before #.  So it's building the string #photos_inner>img:eq(23) where 23 is the random number from next.  

$(this).scrollTo(
 
 '#photos_inner>img:eq(' + next + ')',
 
 {duration: 1000}
 
 );

0
 
charmingduckAuthor Commented:
so   next       is  integer



+ next +        is string?


but eq(3)  also works, that means  3  here is string?
0
 
nap0leonCommented:
I answered this already... perhaps you missed it?

The expression to be executed, looks like this when it is actually processed::
$(this).scrollTo( '#photos_inner>img:eq('123456')', {duration: 1000} );

The way you build that expression is to put single-quotes around the text:
To pass this into a formula,:
#photos_inner>img:eq('123456')

you build it like this:
'#photos_inner>img:eq(' + next + ')'
(where next=1234657)
0
 
nap0leonCommented:
sorry - left in an extra set of single-quotes in my explanation.
The function when processed looks like this:

$(this).scrollTo( '#photos_inner>img:eq(123456)', {duration: 1000} );


to build the "'#photos_inner>img:eq(123456)'" part of it,
'#photos_inner>img:eq(123456)'
You use
'#photos_inner>img:eq(' + next + ')'
 (where next = 1234567)
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 5
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now