Imnotapotato
Imnotapotato

Reputation: 5808

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

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. )

Upvotes: 3

Views: 4348

Answers (4)

M&#225;rcio Brasil
M&#225;rcio Brasil

Reputation: 31

$('body').find('.teads-inread:not(:last)').hide();

https://jsfiddle.net/mbrasil/n0reckgv/

Upvotes: 0

guest271314
guest271314

Reputation: 1

Try

$(".dyn li span.expCollPos").slice(-1).hide();

$(".dyn li span.expCollPos").slice(-1).hide()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<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">1</span>
        </span>
        <span class="expCollPos linkItem" >
            <span class="collapsed">2</span>
        </span>
        <span class="expCollPos" >
            <span class="collapsed">3</span>
        </span>
    </li>


    <li id="licategory_2">

    </li>

</ul>

Upvotes: 2

Arun P Johny
Arun P Johny

Reputation: 388316

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();

Upvotes: 8

I&#39;m Geeker
I&#39;m Geeker

Reputation: 4637

Use this

$('#licategory_1 span.expCollPos').eq(2).show();

Upvotes: 1

Related Questions