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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With