jQuery: Select random nth element

How can I select a random matching element?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta charset="utf-8" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
 $('p:eq(rand)').css('border','2px solid red');

Open in new window

LVL 16
Who is Participating?
Test page : http://jsfiddle.net/Ny5TV/

$(document).ready(function() {
 var rand = parseInt(Math.random() * $('p').length);
 $('p:eq(' + rand + ')').css('border','2px solid red');

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.