jQuery – Excluding elements with attributes ending in a particular string

Ok, let’s say you have some data that looks like this:

A title
Some content
A title
Some content

Now, if you need to style just the elements that are NOT post-modal, you have a problem. You can’t just select elements that start with an id value of ‘post,’ because then you will be selecting all the divs within the container. You could style the html with an additional class, but in this case the html wasn’t changeable, and adding a class with jQuery presents the same problem as selecting the elements–they’ll all match at the start of the id, even the ones we don’t want. The solution is this:

$(".container").children().not('[id$=modal]').each(function(){
//you can do something to each non-modal element in here
});

Now we’ve selected only the elements within our container that don’t contain the word ‘modal’ at the end, and we’re free to do whatever we need to them (in my case, it was pagination). For additional info on the .not() method, check out the jQuery doc.

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *