Evil_skunk
Evil_skunk

Reputation: 3392

JQuery Selector for not "wrapped" elements

I'm looking for a JQuery Selector which select all img elements which are not wrapped by a specific div (with class .special or .dontSelect)

For example my html code:

<div class="special">
    <ul>
        <li>
             <img id="1"></img>
        </li>
    </ul>
</div>
<div>
    ....
          <img id="2"></img>
    ....
          <div class="dontSelect">
              <img id="3"></img>
          </div>
</div>

In this example only the img with id 2 should be selected, because 1 and 3 are "wrapped".

Thanks for help

Upvotes: 3

Views: 522

Answers (3)

i--
i--

Reputation: 4360

Try:

 $('*:not(.dontSelect), *:not(.special)').find('img')

or even

 $('*:not(.dontSelect, .special) img')

Upvotes: 0

Esailija
Esailija

Reputation: 140228

There is no selector for that but you can select all images and filter them:

$("img").filter( function(){
    return !$(this).closest(".special,.dontSelect").length
}).remove();

//Will remove the image with id="2"

Upvotes: 4

Ram
Ram

Reputation: 144699

You can use the filter method:

var $imgs = $('img').filter(function(){
               return $(this).closest('.dontSelect, .special').length === 0
           })

Upvotes: 1

Related Questions