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
LVL 5
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: http://jsfiddle.net/AlexCode/v2d4gg1y/
function rndApplyClass(ulId, className){
    var $ul = $('#' + ulId),
        $liList = $ul.find('li'),
        rndIndex = Math.floor(Math.random() * $liList.length),
        $rndItem = $($liList[rndIndex]);
    
    $liList.removeClass(className);
    $rndItem.addClass(className);
}

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 :)
http://jsfiddle.net/AlexCode/v2d4gg1y/1/
0

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>
<html>
<head>
<title>Test</title>
<style type="text/css">
li.someclass {
  color: red;
  font-size: 20px;
}
</style>
</head>
<body>
<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>
</ul>
<script src="http://code.jquery.com/jquery.js"></script>
<script type="text/javascript">
$(function() {
  $('a').click(function(e) {
    e.preventDefault();
    randomLi('xyz');
  });
});
function randomLi(id)
{
  var indx = parseInt($('#' + id +' li').length * Math.random());
  $('li.someclass').removeClass('someclass');
  $('#' + id + ' li:eq(' + indx + ')').addClass('someclass');
}
</script>
</body>
</html>

Open in new window

0
Neil_BradleyWeb UX/UI DeveloperAuthor Commented:
Thanks to both of you for submitting solutions however this one exceeded.
Great work.
Cheers,
Neil
0
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
jQuery

From novice to tech pro — start learning today.

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.