How to hide last <li> using JQuery but not the nested <li>s

I have a simple nested menu example and I would like to hide the last top level <li> which in the below example is the <li> containing the menu "Sustainability report". Unfortunately my JQuery script seems to affect the nested <li> tags as well which is not what I want.

Incidentally I cannot add an ID to the last <li> which I realise would solve the problem but a solution needs to be applied to the HTML as shown without any modifications.

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Hide last li</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>

<script type='text/javascript'>
//<![CDATA[
$(function() {

  $('#main-navigation ul li:last-child').hide();
	  
});
//]]>
</script>

</head>

<body>
<div id="main-navigation">
  <ul>
    <li>
      <a>About us</a>
      <div>
        <ul>
          <li>
            <a>Our Vision</a>
            <ul>
              <li><a href="#">Vision statement</a></li>
            </ul>
          </li>
        </ul>
        <ul>
          <li><a>Report and Accounts</a>
            <ul>
              <li><a href="#">Chief Executive's Statement</a></li>
              <li><a href="#">Documents</a></li>
              <li><a href="#">History</a></li>
            </ul>
          </li>
        </ul>
      </div>
    </li>
    <li><a>Our work</a>
      <div>
        <ul>
          <li><a>Current Projects</a>
            <ul>
              <li><a href="#">Project 1</a></li>
              <li><a href="#">Project 2</a></li>
              <li><a href="#">project 3</a></li>
            </ul>
          </li>
        </ul>
        <ul>
          <li><a>Sectors</a>
            <ul>
              <li><a href="#">Aviation</a></li>
              <li><a href="#">Energy</a></li>
              <li><a href="#">Flood Alleviation</a></li>
            </ul>
          </li>
        </ul>
      </div>
    </li>
    <li><a>Sustainability report</a>
      <div>
        <ul>
          <li><a title="Menu 1">Menu 1</a>
            <ul>
              <li><a href="#" title="Menu 1 - 1">Menu 1 - 1</a></li>
              <li><a href="#" title="Menu 1 - 2">Menu 1 - 2</a></li>
            </ul>
          </li>
        </ul>
      </div>
    </li>
  </ul>
</div>
</body>
</html>

Open in new window

mike99cAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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

Duy PhamFreelance IT ConsultantCommented:
Maybe this could help to solve your case:

$('#main-navigation ul:first-child li:last-child').hide();

Open in new window

Rainer JeschorCommented:
Hi,
just change the selector to this:
  $('#main-navigation > ul > li:last-child').hide(); 

Open in new window


Adding the ">" between the selectors will select the nested correct element.

Sample:
http://jsfiddle.net/qep31x6w/

HTH
Rainer

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
mike99cAuthor Commented:
Thanks this worked perfectly. The use of ">" for immediate children did the trick.
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.