Solved

what does this mean

Posted on 2011-09-28
13
214 Views
Last Modified: 2012-06-27
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
Comment
Question by:charmingduck
  • 5
  • 3
  • 3
  • +2
13 Comments
 
LVL 18

Expert Comment

by:nap0leon
ID: 36716503
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
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36716525
// 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
 
LVL 40

Expert Comment

by:gurvinder372
ID: 36716531
It is simply going to scroll to the image whose index is randomly selected
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

by:charmingduck
ID: 36716641
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
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36716658
eq() needs string not integer
0
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36716675
Sorry My mistake
0
 

Author Comment

by:charmingduck
ID: 36716718
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
 
LVL 17

Assisted Solution

by:sonawanekiran
sonawanekiran earned 31 total points
ID: 36716736
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
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36716749
And if you use

'#photos_inner>img:eq(next)'

the variable next is not evaluted
0
 
LVL 15

Assisted Solution

by:tim_cs
tim_cs earned 31 total points
ID: 36716759
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
 

Author Comment

by:charmingduck
ID: 36716798
so   next       is  integer



+ next +        is string?


but eq(3)  also works, that means  3  here is string?
0
 
LVL 18

Accepted Solution

by:
nap0leon earned 63 total points
ID: 36716852
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
 
LVL 18

Assisted Solution

by:nap0leon
nap0leon earned 63 total points
ID: 36716884
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Jqeury/JS: Mutiplying the difference in 2 dates and comparing to another number 2 30
VBScript on Html 15 47
jquery tab header text 1 23
Search Item in Table 2 23
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question