Add a class to a random li tag

Hi all,
I would like to add a class to a randomly chosen li tag.

For example a ul tag with the id <ul id=test>. Add class="show" on one of its li tags within the list.
Suggestions welcome
Neil_BradleyWeb UX/UI DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alexandre SimõesManager / Technology SpecialistCommented:
Here's a demo:
function rndApplyClass(ulId, className){
    var $ul = $('#' + ulId),
        $liList = $ul.find('li'),
        rndIndex = Math.floor(Math.random() * $liList.length),
        $rndItem = $($liList[rndIndex]);

Open in new window

It's generic enough so you can use it withouth having to change the function itself.

Here's the same code but with a setInterval so you can see it randomly changing :)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Julian HansenCommented:
Not to take away from Alexndre's solution - here is an alternative
Working sample here
<!doctype html>
<style type="text/css">
li.someclass {
  color: red;
  font-size: 20px;
<a>Click Me</a>
<ul id="xyz">
  <li>Item 1</li>
  <li>Item 1</li>
  <li>Item 1</li>
  <li>Item 1</li>
  <li>Item 1</li>
  <li>Item 1</li>
<script src=""></script>
<script type="text/javascript">
$(function() {
  $('a').click(function(e) {
function randomLi(id)
  var indx = parseInt($('#' + id +' li').length * Math.random());
  $('#' + id + ' li:eq(' + indx + ')').addClass('someclass');

Open in new window

Neil_BradleyWeb UX/UI DeveloperAuthor Commented:
Thanks to both of you for submitting solutions however this one exceeded.
Great work.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.