views:

89

answers:

4
    this.randomtip = function(){
     var length = $("#showcase ul li").length;
     var ran = Math.floor(Math.random()*length) + 1;
     $("#showcase ul li:nth-child(" + ran + ")").show();
    };
    randomtip();

..the code above works well with .show() in the end, but if I change it to .addClass('show') it won't work.

I'd like to give it a class instead to apply float:left property to all li items instead of the default display:list-item. How do I fix this?

Thanks!

A: 

How are you hiding the element in the first place? If you're setting it to display: none, for example, or using jQuery's hide(), which does the same thing, then that will override CSS defined elsewhere and keep your element hidden until you remove it. It ought to work if you use both .show() (to remove display: none) and .addClass('show') (to add your CSS class).

Jordan
I'm using display:none on LI's, yes. What do I need to change?
Nimbuz
A: 

Try replacing

$("#showcase ul li:nth-child(" + ran + ")").show();

with this one

$("#showcase ul li:nth-child(" + ran + ")").addClass('show').show();
jitter
That almost solves the problem, technically, but the other li's are still hidden. I'd like this code to show atleast 7 li's every rotation, what do I change in the code? Thanks!
Nimbuz
Huh? To be honest I don't have the feeling your comment relates to the question you asked or the information you provided in there. What do you mean with "every rotation" and "atleast 7 li's". Please elaborate the question on what you really are trying to achive. What your code at the moment does is show 1 random `li` according to your selector
jitter
A: 

After Jordan's answer:

You can replace the display:none from li and then add the class this way:

$("#showcase ul li:nth-child(" + ran + ")").css('display','').addClass('show');
Anis
A: 

instead of display: none, use .addClass('hidden') / .removeClass('hidden')

just somebody