Requirement of Selecting the Entire Unordered List using JQuery

Introduction

Today I came to work on a requirement of selecting the entire li from a ul but not the last one. So, I thought to share that info with you. Hope you will like it.

Using the code

You can see the jsfiddle here: JSFIDDLE.

Here I have ul list items as follows;

  1. <ul>  
  2.    <li>Sibeesh</li>  
  3.    <li>Sibi</li>  
  4.    <li>SibeesPassion</li>  
  5.    <li>Buddy</li>  
  6.    <li>NA</li>  
  7. </ul>  
  8. <ul>  
  9.    <li>Sibeesh</li>  
  10.    <li>Sibi</li>  
  11.    <li>SibeesPassion</li>  
  12.    <li>Buddy</li>  
  13.    <li>NA</li>  
  14. </ul>  
  15. <ul>  
  16.    <li>Sibeesh</li>  
  17.    <li>Sibi</li>  
  18.    <li>SibeesPassion</li>  
  19.    <li>Buddy</li>  
  20.    <li>NA</li>  
  21. </ul>  
In that list I have mentioned my nick names , please be noted that I have mentioned the last li as NA, which means I don’t want the JQuery selector to select that particular li item.

Here I am appending “--My Nick Name” to the li items, except to the last one.
  1. $('ul li:not(:last-child)').append(" --My Nick Name ");  
So here is my output

Output

  • Sibeesh --My Nick Name
  • Sibi --My Nick Name
  • SibeesPassion --My Nick Name
  • Buddy --My Nick Name
  • NA
  • Sibeesh --My Nick Name
  • Sibi --My Nick Name
  • SibeesPassion --My Nick Name
  • Buddy --My Nick Name
  • NA
  • Sibeesh --My Nick Name
  • Sibi --My Nick Name
  • SibeesPassion --My Nick Name
  • Buddy --My Nick Name
  • NA
  • Points of Interest
  • JQuery
  • Historyy
  • 1st Version: 12-12-2014
Ebook Download
View all
Learn
View all