Darf Zon
Darf Zon

Reputation: 6378

JQuery: exclude one element of the children

I'm trying to get all the next html code and excluding the DIV with id fiscal-address-info using jquery. I started using not method but no success.

This is the original snippet code.

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

What I'd like to get in the jquery result is:

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

This is what I've tried:

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

Upvotes: 1

Views: 1401

Answers (4)

user3364886
user3364886

Reputation: 13

Try this

$('#fiscal-info').children(':not(#fiscal-address-info)');

Upvotes: 0

Alnitak
Alnitak

Reputation: 339786

If you want to reproduce that branch of the DOM tree, excluding the unwanted child, use:

var $copy = $('#fiscal-info').clone();       // deep copy
$copy.find('#fiscal-address-info').remove(); // remove child

You can then do whatever you want to $copy.

Note however that converting it back into HTML so you can subsequently re-insert it into the DOM is almost never the right thing to do. HTML is only a mechanism for transfer of DOM structures - subsequent manipulation of DOM structures should be done using DOM methods.

Upvotes: 1

user1618236
user1618236

Reputation:

You need to get the children of your first element, try this:

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

If you want the parent too you can try this

var myHtml =  $('#fiscal-info').clone(false)
myHtml.children.remove('#fiscal-address-info');

Upvotes: 1

Elliot Bonneville
Elliot Bonneville

Reputation: 53291

Getting #fiscal-info and its children, excluding the child with the ID #fiscal-address-info:

var collection = $("#fiscal-info").children.not("#fiscal-address-info").add("#fiscal-info");

Upvotes: 0

Related Questions