Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide all elements except the last one (by class) using jQuery

Tags:

jquery

css

I have a menu that looks like this (1 list item as an example):

<ul class="dyn makeLink" style="display: block;">
    <li id="licategory_1">
        <a href="/nfl-lines" title="" class="linkItem">
            <strong>NFL</strong>
        </a>
        <span class="expCollPos" >
            <span class="collapsed"></span>
        </span>
        <span class="expCollPos linkItem" >
            <span class="collapsed"></span>
        </span>
        <span class="expCollPos" >
            <span class="collapsed"></span>
        </span>
    </li>


    <li id="licategory_2">
    ... 
    </li>

</ul>

Which has for some strange reason 3 spans(.expCollPos), the two first ones aren't relevant for me and I'm trying to remove ONLY them using jQuery.

I tried using: $('.dyn li span.expCollPos:last-child').css("display", "none");

and several others - but it just removes all of the .expCollPos classes.

Am I doing something wrong?

(I got a code that I have to edit and it looks horrable! The javascript functions are unclear and the CSS has so much "!important" that I cant find what's what. )

like image 836
Imnotapotato Avatar asked Jan 28 '26 03:01

Imnotapotato


1 Answers

You want to hide all but the last one, so you have to say not last like

$('.dyn li').find('span.expCollPos:not(:last)').hide();
like image 65
Arun P Johny Avatar answered Jan 30 '26 20:01

Arun P Johny