0

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!

3 Answers 3

3

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

$('.related3').load('inc/related.asp .list:eq('+rand+')');
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

This should do:

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

2 Comments

Be careful with having both single and double quotes here, as that will turn the value of rand into a string (which will break this)
@MFazio23 There'll be no problem as rand will output integer and eq will accept string...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.