Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select every element that is not inside another element

<img src="xxx" alt="xxx" title="xxx">
<div class="buttons">
    <a href="xxx"><img src="xxx" alt="xxx" title="xxx"></a>
</div>

I need to write a jQuery selector, that will select ONLY images with title attributes that are OUTSIDE the .buttons div. I know that to select images with title attributes I need to use something like this:

$("img[title]")

I know that there is something like :not() selector in jQuery, but I can't find the way to combine them together to achieve that exact result.

like image 974
Pirozek Avatar asked Nov 23 '10 17:11

Pirozek


People also ask

How do you select all elements but one in CSS?

The :not() selector selects all elements except the specified element. This is mostly used together with another selector to select everything except the specified element in a group (like in the example above).

What CSS selector should we use if we want to select all elements but not the specified class or selector?

For selecting everything but a certain element (or elements). We can use the :not() CSS pseudo class.

What will be selector to select all the elements?

The * selector selects all elements. The * selector can also select all elements inside another element (See "More Examples").

How do you target something inside or around another element in CSS?

The :target-within pseudo-class can be used to highlight the article if anything inside it has been directly linked to. The :target pseudo-class is also being used to show which item has been targeted.


1 Answers

You can get the result set then filter it using .not(), like this:

$("img[title]").not(".buttons img")

Or filter in the same selector using :not() (but this is probably a bit slower in older browsers), like this:

$("img[title]:not(.buttons img)")
like image 127
Nick Craver Avatar answered Oct 15 '22 16:10

Nick Craver