sullivansg
sullivansg

Reputation: 29

Jquery load - using variable in selector

I can't get the rand variable to work in this code below:

$(document).ready(function() {
   var rand = Math.floor((Math.random() * 10) + 1);      
   $('.related1').load('inc/related.asp .list:eq(1)');
   $('.related2').load('inc/related.asp .list:eq(2)');
   $('.related3').load('inc/related.asp .list:eq(rand)');
});

The first 2 load fine, but the one using the random number does not.

I also saw a suggestion and tried:

   $('.related3').load('inc/related.asp .list:eq("+rand+")');

But that didn't work either. Any ideas? Thanks in advance!

Upvotes: 0

Views: 39

Answers (3)

Bhojendra Rauniyar
Bhojendra Rauniyar

Reputation: 85575

This should do:

$('.related3').load('inc/related.asp .list:eq("'+rand+'")');

Upvotes: 0

Sasanka Panguluri
Sasanka Panguluri

Reputation: 3128

Do this:

 $('.related3').load('inc/related.asp .list:eq('+rand+')');

You started the argument inside load with ', so you need to terminate it with another ' and append rand to that string, and append this string ')' back.

Upvotes: 0

MFazio23
MFazio23

Reputation: 1312

You just have a mismatch with your quotes. Do something like this:

$('.related3').load('inc/related.asp .list:eq('+rand+')');

Upvotes: 3

Related Questions