Fiodor
Fiodor

Reputation: 806

How to select all the divs contained within same parent-div except one

How do I select all the divs within one parent-div except one? Let's say I have code like this:

<div id="parent">
    <div id="child1"></div>
    <div id="child2"></div>
    <div id="child3"></div>
</div>

And I want to select #child1 #child2. Of course I could just select them normally, but let's say I have many child-divs.

P.S. I don't want any selector like last or something since it's not said it has to be always the last that I want to skip.

Upvotes: 0

Views: 1072

Answers (4)

Volkan Ulukut
Volkan Ulukut

Reputation: 4218

you can use not method.

$("#parent > div").not("#child3")

this will select all direct children of parent div except child3

Upvotes: 2

$('#parent > div').not('#child3');

or

$('#parent > div:lt(2)').

or

$('#parent > div:not("#child3")');

Upvotes: 2

Paulie_D
Paulie_D

Reputation: 115045

You can use the :not pseudo-class in CSS...no need for Jquery

JSFiddle Demo

#parent div:not(#child3) {
  /* your styles here */
}

Upvotes: 1

Felix
Felix

Reputation: 38102

You can use not():

$('#parent div').not('#child3')

or :not selector:

$('#parent div:not("#child3")');

This will select all your child divs inside parent div except #child3 element.

Upvotes: 3

Related Questions