ofimbres
ofimbres

Reputation: 31

jQuery selecting and exclude an element inside a div

I'm trying to get all the next html code and excluding the DIV with ID=fiscal-address-info using jquery. I started using filter method but no lucky.

This is the original snippet code.

<div id="fiscal-info" class="content-fields">
    <div class="content-title"><h2>Datos Fiscales</h2></div>
    <div class="row">...</div>
    <div class="row">...</div>
    <div class="row">...</div>
    <div id="fiscal-address-info">
    <div class="row">...</div>
    <div class="row">...</div>
</div>

What I'd like to get is:

<div id="fiscal-info" class="content-fields">
    <div class="content-title"><h2>Datos Fiscales</h2></div>
    <div class="row">...</div>
    <div class="row">...</div>
    <div class="row">...</div>
    <div class="row">...</div>
    <div class="row">...</div>
</div>

This is what I've tried:

$('#fiscal-info').filter('#fiscal-address-info');

Upvotes: 3

Views: 4095

Answers (4)

Hannan Hossain
Hannan Hossain

Reputation: 740

You can use the .not() method or :not() selector

http://api.jquery.com/not-selector/

Code based on your example:

$('div#fiscal-info div').not("#fiscal-address-info");

Have a look into this fiddle

Upvotes: 2

j08691
j08691

Reputation: 208040

$('#fiscal-info div').filter(function () {
    return this.id == 'fiscal-address-info'
}).remove();

jsFiddle example

Upvotes: 0

Andrea Parodi
Andrea Parodi

Reputation: 5614

You can use the filter method and using a not selector:

$("#fiscal-info").children().filter(":not(#fiscal-address-info)")

This return you all the fiscal-info children ,except the excluded one.

Upvotes: 5

Mr Boss
Mr Boss

Reputation: 472

 $( "div" ).filter( $( "#fiscal-address-info" ) );

Gets all div elements and filters them for id #fiscal-address-info Hope this helps!

Upvotes: 0

Related Questions