0

Okay so I have a bunch of tables in my website they all have the same class which is 'item -' and what ever number the table is here is how I did that:

$("table").each(function(index){
 $(this).addClass('item-'+index);
 });

Now what im trying to do is make it so if the class is anything higher than 'item-8' to make the visibility hidden i tried something like this but it doesn't work. am I in the right track or completely off?

$('table').each(function(index){
      if($(this).hasClass('item-'index) > 8){
        $(this).css('visibility','hidden');
      }
 });

3 Answers 3

1
$("table").each(function(index) {
    $(this).addClass('item-' + index);
    if ( index > 8 ) {
        $(this).css('visibility', 'hidden');
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

@RodrigoLessa - There was a typo in my code (visibility is spelled with one l). I updated it.
They all worked but i chose this answer because you did both my functions in one
0
$('div[class^=item-]').filter(function(e){
    return e >7;
}).css('visibility','hidden');

Quick jsFiddle example using divs.

2 Comments

There's no guarantee the the item-* class name is the first one.
@JosephSilber - correct, there isn't. But if there is just the one class it works fine.
0

Why not just use css for this? Give each item an "item" class. Provided you don't need IE8 support for this, this just becomes:

.item:nth-child(n+8) {
    visibility:hidden;
}

jQuery supports nth-child so you can also use that as a selector instead of filter. http://api.jquery.com/nth-child-selector/

See also: http://css-tricks.com/how-nth-child-works/ and http://jsfiddle.net/gwwar/nneQj/ for a quick fiddle.

Comments

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.