Find the final element that does not include a particular class

Looking for the best way to identify the last item in a list without a specific class? Learn how to write code that can check for the absence of a class name.

$('ul li:last')
$('ul li:not(.special)')

Answer №1

To easily select the last item in a list using jQuery, you can utilize the last() function. Here's how you can do it:

// Reference: $("ul li:not('.y')").last().text()
document.write('The last non `y` class contains the text: ' + $("ul li:not('.y')").last().text() );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<ul>
  <li class="t">Not me.</li>
  <li class="y">Not me.</li>
  <li class="t">Not me.</li>
  <li class="y">Not me.</li>
  <li class="t">Not me.</li>
  <li class="y">Not me.</li>
  <li class="t">This one please!</li>
  <li class="y">Not me.</li>
</ul>

For more information, you can refer to the documentation here: https://api.jquery.com/last/

Update

You can also use nested CSS selectors as suggested by @RejithRKrishnan in the comments, like so:

// Reference: $("ul li:not('.y')").last().text()
document.write('The last non `y` class contains the text: ' + $("ul li:not('.y'):last").text() );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<ul>
  <li class="t">Not me.</li>
  <li class="y">Not me.</li>
  <li class="t">Not me.</li>
  <li class="y">Not me.</li>
  <li class="t">Not me.</li>
  <li class="y">Not me.</li>
  <li class="t">This one please!</li>
  <li class="y">Not me.</li>
</ul>

Answer №2

Perhaps this solution could be of assistance. The variable ele will store the last element within a list that does not have a specific class applied to it.

 var ele;
  $('ul li').each(function(){
     if(!$(this).hasClass("specificClass"))
      {
           ele = this;
      }
  });

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Building a Responsive Grid Layout with HTML and CSS: Layering one div on top of another

I'm struggling to find a solution to my problem. I attempted to set my div as absolute but it messed up the entire flexible grid layout. All I need is to place the div above the background div. Thank you in advance! CSS #mannequin { box-sizing: ...

Revise Total Amount in Woocommerce Checkout Using an Ajax Call

Before seeking help on SO, I researched extensively to understand how to make an AJAX request with WordPress. Although my code and request are functioning correctly, they are not achieving the desired outcome. My goal is to update the "Total" value when c ...

The parent element of a 3D div is causing issues with hovering and clicking on the child elements

In my scenario, the parent div is transformed in 3D with rotation, causing it to move to the backside. The issue arises with the child div containing a button that becomes unclickable due to the parent div position. Setting backface-visibility to hidden al ...

When resizing the window, the click events are getting stuck and not releasing the click

I'm attempting to implement a dropdown menu using jQuery on a click event. Here is the code snippet: $(".sidebar-nav li > a").click(function(e) { $(this).parent().siblings().find('ul').slideUp(500); $(this).next('ul& ...

The functionality of data attributes in jquery mobile is not functioning correctly

Within my header, I have included the following code: <link rel="stylesheet" type="text/css" href="css/jquery.mobile-1.4.5.css"> <script type="text/javascript" src="js/jquery-1.11.2.js"></script> <script type="text/javascript" src="js ...

Is there a way to enable a clickable imagemap area using jQuery?

Example showcasing different behaviors in various browsers (jsfiddle link): <!DOCTYPE html> <html> <head> <title>Browser Behavior Test</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/ ...

The scrolling content is positioned beneath the primary header and navigation bar

I'm currently in the process of designing a website where, upon clicking a navigation button, part of the page scrolls while the top header & nav bar remain fixed. Although I've managed to get the scrolling functionality to work and keep the top ...

Are there any jQuery plugins available that can display a loader image while a page is loading?

After extensive research on Google, I have been unable to find the plugin I need. If you are aware of any suitable plugins, please let me know. Thank you in advance! ...

Tips for swapping out the content within a "li" element

I am dealing with a situation where I have approximately 100 pages, all containing an <ul> tag, but the issue is that I need to enclose each list item within a <span> tag. Below is the code snippet I have tried: <ul style="list-style-type: ...

Ways to modify font color in JavaScript "type"

Recently, I came across a fascinating technique where by simply refreshing the page, the text changes sentences. I managed to implement the code successfully, however, I am having trouble changing the color, size, and alignment of the text. <script type ...

Analyzing neglected CSS in intricate websites

While there are tools available to identify unused CSS on static web pages, real-world scenarios often involve CSS being used dynamically after interactions such as opening modals or popups. How can we effectively manage our ever-growing render-blocking CS ...

Implementing a splash screen upon selecting the second exit option

Check out the code snippet here: https://jsfiddle.net/wust7gdy/ Once the curtain is raised, the exit button will appear. How can I introduce a splash screen after clicking the 2nd exit button? Currently, there is a splash screen that appears after click ...

Retrieving array values from various fields with identical CLASS using jQuery and utilizing AJAX to transmit the information to PHP

I need to extract values from multiple fields with the same CLASS using JavaScript and then utilize AJAX to send the data to PHP <input type="text" class="form-control subject" placeholder="Subject name"> <input t ...

Executing JavaScript code using an HTML form submission

Greetings, I have implemented an HTML form on my website for user login using AJAX (jQuery), but I am encountering some issues. Below is the JavaScript code: function validateLoginDetails() { $('[name=loginUser]').click(function() { ...

An issue arises in vue.js where the v class binding takes precedence over other bindings and fails to properly remove

I have a game with a punching bag where I want to incorporate an animation class each time the bag is clicked. Once the health meter hits zero, I intend to replace the bag image with one depicting a burst bag. Things are running smoothly up until the poin ...

Challenges arise when utilizing CSS3 animations in conjunction with transitions triggered by toggling a JavaScript class

Struggling to activate an animation while updating a class using JavaScript for a PhoneGap app. Planning on utilizing -webkit- prefixes for compatibility. However, the animations are currently unresponsive in Chrome during testing, both when applied to th ...

Restrict Bootstrap Column Width to Container Boundary

I am struggling to create a two-row hierarchy display for a set of photos using bootstrap 5. Despite my efforts, the images overflow out of the container instead of staying within it. My goal is to make the container fill the screen and act as a cover disp ...

Locate a particular text element following a specific anchor tag using jQuery

Consider the scenario: <a href="foo">Subject</a> &gt; <a href=bar">Negotiation</a>, 1) Can you target the "&gt"; that comes after this specific a tag using jquery? I only want to select certain occurrences, not all on the ...

Unveiling the Power of Angular in Handling Date and Time

Recently, I encountered an issue with the following code snippet: <ng-template>{{date:'MM/dd/yyyy h:mm:ss a Z'}}</ng-template>. This code displays a date and time in one long line. My goal is to insert a line break between the date an ...

How to easily toggle multiple elements using jQuery

I'm having trouble getting this block of code to function properly: $("a.expand_all").on("click",function(){ $(this).text('Hide'); $('.answer').each(function () { $(this).slideDown(150, function () { $( ...