Josh Scott
Josh Scott

Reputation: 3850

How do I exclude elements whose ID ends in a certain suffix using jQuery

I have this jQuery function:

$(this).change(function(){
  alert('I changed. ID: ' + $(this).attr("id"));
});

I need the alert to fire except when the id name ends in -0. I think I should be using the $= operator. I cannot figure out how to make it work.

Upvotes: 0

Views: 1040

Answers (2)

Nick Craver
Nick Craver

Reputation: 630589

Your selector should use :not() combined with attribute-ends-with ($=) look like this:

$(":not([id$='-0'])")

It's better to have something in front of that so it doesn't run against every element, a class or an element tag, etc, like this:

$(".myClass:not([id$='-0'])")

Upvotes: 5

Christian Benincasa
Christian Benincasa

Reputation: 1215

try something like this

$("id:not[id$='-0']");

with an if statement.

Upvotes: 1

Related Questions