Select items that do not have classes containing the string.

I need a jQuery selector that selects any elements that do not have a class containing the string '-text';

For example, in the following:

<img class="cow" > <img class="monkey" > <img class="cow-text" > <img class="monkey-text" > 

I would like to choose:

 <img class="cow" > <img class="monkey" > 

I know that I need to use not , but I'm not sure about this format. The following results will not be selected:

$('.panel').find('.layer').not('[class*="-text"]');

+4
source share
3 answers

If the class always ends with "-text", you should use the EndsWith selector, for example

 panel.find('img').not('[class$="-text"]'); 

or

 panel.find('img:not([class$="-text"])') 

This is a more accurate solution, the wildcard selector means that classes such as some-text-class or -text-class will also be mistaken.

+2
source

Try

 panel.find('img').not('[class*="-text"]'); 
+1
source

Try to find all img elements and delete everything at the end with -text . And use each for multiple items. for example class = "monkey-text"

 $("img").not("[class$='-text']").each(function(){ alert($(this).attr("class")); }); 

The second preview shows the line text- at the beginning of the line. Just replace $ with ^ for example class = "text-monkey"

 $("img").not("[class^='text-']").each(function(){ alert($(this).attr("class")); }); 
0
source

Source: https://habr.com/ru/post/1495317/


All Articles