How to Select All Elements With a Class Except One Using CSS

How to Select All Elements With a Class Except One Using CSS

To select all elements with the same class except a particular one using jQuery, you can use the not method in combination with the class selector.

Here’s an example:

<div class="example">Element 1</div>
<div class="example">Element 2</div>
<div class="example">Element 3</div>
<div class="example" id="exclude">Element 4</div>
<script>
$(document).ready(function() {
var excluded = $(“#exclude”);
$(“.example”).not(excluded).css(“background-color”, “yellow”);
});
</script>

In this example, we first select the element that we want to exclude using its id selector, and store it in a variable called excluded. Then, we use the class selector .example to select all elements with the class example, and use the not method to exclude the element stored in the excluded variable. Finally, we use the css method to change the background color of all elements except the excluded one.

The not method allows us to exclude elements from a set of selected elements, making it possible to select all elements with a certain class except for a specific one.

You can also use the following to select all element except for the current one –

jQuery('.arrowm').not(this)

Al Mahbub Khan
Written by Al Mahbub Khan Full-Stack Developer & Adobe Certified Magento Developer

Full-stack developer at Scylla Technologies (USA), working remotely from Bangladesh. Adobe Certified Magento Developer.