Solved

what does this mean

Posted on 2011-09-28
13
215 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article discusses how to implement server side field validation and display customized error messages to the client.
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…

734 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