dhk628 dhk628 - 3 months ago 23
Javascript Question

variable in nth-child() jquery / javascript

i have the following code:

var max_count = $(".slider").children().length;
function fn_get_natural_dim(slide_image,img){
var width = img.width;
var height = img.height;
var ratiow = width/600;
var ratioh = height/400;

height = height/ratiow;
var margin = (400-height)/2;
width = width/ratioh;
var margin = (600-width)/2;
for(var count=1;count<=max_count;count++)
var count_string = count.toString();
var img_name = "img" + count_string;
var slide_image = $('.slider > li:nth-child(" + count + ")');

var img = new Image();
img.onload = (function(slide_image,img){
return function() {fn_get_natural_dim(slide_image,img);};
img.src = $(slide_image).attr("src");

i want the variable slide_image to select the nth list, where n should be the same as the variable count.
i added the + signs (although i dont know why i have to) and made the quote symbols different, and yet it still doesnt work


You should do it like this:

$('.slider > li:nth-child(' + count + ')');

The problem is that you're not closing the strings/segments surrounding the variable, so:

'.slider > li:nth-child(" + count + ")' 

Is just a long string, a selector which makes no sense; that's why it matches nothing, you have to actually separate the sections by ending the static parts with the same kind of quote...