jQuery :not() Selector Example

It’s been a while since I wrote about JQuery. I am spending most of my time these days on backend technologies. Recently while working on a typical requirement on UI, I had to play with some tricky JQuery selectors. Typically, most of the times we use JQuery’s class selectors or id selectors for example:

$('#someid').hide(); $('div#container').css('height', '100%'); $('.someclass').hide(); $('div.entry').css('height', '100%');
Code language: JavaScript (javascript)

But recently I had a requirement where I had to select all the elements from DOM and ignore a certain one. Consider following example: In below table, we have checkboxes at each row level. Also there a checkbox to “select all” in header.

Also consider each checkbox has different class and id. Thus it is not possible to select all the row’s checkboxes and ignore ‘select all’ checkbox.

jQuery :not() Selector

In scenario like above and many others, JQuery’s :not() selector comes quite handy. It helps in filtering certain conditions from your selector query. It simple means “Selects all elements that do not match the given selector”. Consider below table, we have few rows with checkboxes. On select of the checkbox the row background color is highlighted. We simply uses $('input[type=checkbox]') to select checkbox and add click event. But that selects the “select all” checkbox too! 

$('input[type=checkbox]').click(function() { if(this.checked == true) { $(this).parent().parent().addClass('selected'); } else { $(this).parent().parent().removeClass('selected'); } });
Code language: JavaScript (javascript)

When clicking “select all” checkbox in above table, the row is highlighted. Now lets see how not() selector can be used here to ignore “select all” checkbox from the selection list. Now check the below demo with not() selector. 

$('input[type=checkbox]:not("#selectall")').click(function() { if(this.checked == true) { $(this).parent().parent().addClass('selected'); } else { $(this).parent().parent().removeClass('selected'); } });
Code language: JavaScript (javascript)

Check the above HTML table, on clicking “select all” no rows are highlighted! This is because we used $('input[type=checkbox]:not("#selectall")') to select the checkboxes. not() can be combined with most of the jQuery selectors to form powerful queries that can be difficult to achieve. Following are few examples:

  • :not('.cssclass') – Ignores all the elements with class=”cssclass” from the selected list
  • :not('#someid') – Ignores the element with id=”someid” from the selected list
  • :not('first-child') – Ignores the first child from the selected list
  • :not('only-child') – Ignores all the elements that are only child of their parents from the selected list
  • :not(':eq(n)') – Ignores the nth element from the selected list
  • :not(':even') – Ignores all even elements from the selected list

Hope this is useful.

View Comments

Share
Published by
Viral Patel
Tags: JQuery jquery tips tricks jquery ui tutorial

Recent Posts

  • Java

Java URL Encoder/Decoder Example

Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…

4 years ago
  • General

How to Show Multiple Examples in OpenAPI Spec

Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…

4 years ago
  • General

How to Run Local WordPress using Docker

Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…

4 years ago
  • Java

Create and Validate JWT Token in Java using JJWT

1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…

4 years ago
  • Spring Boot

Spring Boot GraphQL Subscription Realtime API

GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…

4 years ago
  • Spring Boot

Spring Boot DynamoDB Integration Test using Testcontainers

1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…

4 years ago