tags:

views:

50

answers:

2

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.

+1  A: 

try something like this

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

with an if statement.

Christian Benincasa
+4  A: 

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'])")
Nick Craver
Can I do this? $(this :not([id$='-0'])) ?
resonantmedia
@resonantmedia - If you you to find element *beneath* `this`, do this: `$(this).find(":not([id$='-0'])") ` if you want to filter the current set use `.filter()` instead, like this: `$(this).filter(":not([id$='-0'])")`.
Nick Craver
Maybe it would help if I give more details, because this is still not working. Here is the whole function.<pre> var divId = 'GPA-Entries'; $("#" + divId).find('select').each(function(){ $(this).change(function(){ if($(":not([id$='-0'])")){ alert('I changed. ID: ' + $(this).attr("id")); } }); });</pre>It is still returning the alert regardless. Even when the div id is: certificationArea-0Please help.
resonantmedia
@resonantmedia - I think it would be easier to filter and just not attach the change handler to those elements, like this: `var divId = 'GPA-Entries'; $("#" + divId ' select:not([id$='-0'])').change(function(){ alert('I changed. ID: ' + this.id); });`
Nick Craver