?
Solved

what does this mean

Posted on 2011-09-28
13
Medium Priority
?
217 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:Gurvinder Pal Singh
ID: 36716531
It is simply going to scroll to the image whose index is randomly selected
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 124 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 124 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 252 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 252 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

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…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…
Suggested Courses

765 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