Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select all elements in DOM apart from one div and the children

I need to select all the elements in the DOM using JQuery/JS, apart from one div. The only issue is, I also need to exempt the divs children in the selection. For example:

<li>1</li>
<li>2</li>
<div>
  3
  <li>3.1</li>
  <li>3.2</li>
</div>

$('*').not('div and children');

//should give me li 1 and 2, but not div 3 or li 3.1/3.2

I've tried the .not() keyword, but it only selects the parent div, and the children are still affected.

like image 734
TechnicalTophat Avatar asked Nov 28 '25 14:11

TechnicalTophat


1 Answers

You can select all elements with $('body *') but not div and not children of div with .not('div, div *')

$('body *').not('div, div *').css('color', 'red');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li>1</li>
<li>2</li>
<div>
  3
  <li>3.1</li>
  <li>3.2</li>
</div>
like image 106
Nenad Vracar Avatar answered Dec 01 '25 02:12

Nenad Vracar