Solved

what does this mean

Posted on 2011-09-28
13
210 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
 

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

705 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now