Exclude an item and its content elements

Ashkan Mobayen Khiabani

There are tons of elements in my page and I'm trying to make only a div including all its content printable.

I have this css:

@media print
{    
    .no-print, .no-print *
    {
        display: none !important;
    }
}

Now before </body> I want to run a jQuery code so that adds .no-print class to every div but .content and its containing elements.

to exclude .content I know what I have to do:

$('div:not(.content)').addClass('no-print');

But to also exclude its content I tried:

$('div:not(.content,.content *)').addClass('no-print');

and

$('div:not(.content),div:not(.content *)').addClass('no-print');

but none of them have the desired result. The code adds .no-print to all of the divs. and printing shows an empty page.

Update: I don't think html would be required in here. but as comments ask for it, I add an example:

<div>
   <div class="header">...</div>
   <div class="nav">...</div>
   <div class="menu>
       <ul>
          <li>menu item 1</li>
          <li>menu item 2</li>
          <li>menu item 3</li>
          <li>menu item 4</li>
          <li>menu item 5</li>
      </ul>       
   </div>
   <div class="sidebar">...</div>
   <div class="content">
       <div><table>....</table></div>
       <div><table>....</table></div>
       <div><table>....</table></div>
  </div>
   <div class="footer">...</div>
</div>
gaetanoM

You may use filter:

var elements = $('div:not(.content) *').filter(function(index, element) {
      return $(this).closest('div.content').length == 0;
});

$(function () {
  var elements = $('div:not(.content) *').filter(function(index, element) {
    return $(this).closest('div.content').length == 0;
  });
  
  elements.each(function(index, element) {
       console.log(index + ' --->  ' + element.outerHTML.substring(0, 30) + '....');
   });
});
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>

<div>
    <div class="header">YES...</div>
    <div class="nav">YES...</div>
    <div class="menu">
        YES
       <ul>
           YES
          <li>menu item 1</li>
          <li>menu item 2</li>
          <li>menu item 3</li>
          <li>menu item 4</li>
          <li>menu item 5</li>
      </ul>
   </div>
   <div class="sidebar">YES...</div>
<div class="content">
    NO
    <div>NO<table>....</table></div>
    <div>NO<table>....</table></div>
    <div>NO<table>....</table></div>
</div>
<div class="footer">YES...</div>
</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Exclude a tag and its content using regex

From Dev

Exclude a tag and its content using regex

From Dev

Flexbox item height independent of its content

From Dev

Python - How to filter a list by the content of its elements?

From Dev

How to prevent a flex item from shrinking smaller than its content?

From Dev

How to hide a list item according to its html content?

From Dev

Using image from sprite in list item after its' content

From Dev

Oneline horizontal list with middle item that fits its content width

From Dev

How do I exclude a clicked item and all its parents from a jquery selector?

From Dev

Exclude item on orderBy

From Dev

grep to file to exclude content

From Dev

Exclude matched array elements

From Dev

Exclude elements from ArrayList

From Dev

Exclude multiple elements in .not, including $this

From Dev

Exclude children elements

From Dev

Exclude multiple elements in .not, including $this

From Dev

Exclude children elements

From Dev

Cannot add distance between droplist and its content that overlaps some other elements

From Dev

How to make up elements which covers the individual cells(which has its own content) of a table in html?

From Dev

How to exclude an item in Django loop?

From Dev

Copy-Item on registry + exclude

From Dev

targeting an element by its content and the content of its ancestors

From Dev

How do I use pseudo-elements to display content after the main item?

From Dev

Exclude Tags Based on Content in Beautifulsoup

From Dev

HTML: content height exclude footer

From Dev

Exclude Tags Based on Content in Beautifulsoup

From Dev

JQuery selector to exclude child elements

From Dev

jQuery: focusout to exclude some elements

From Dev

exclude form elements from getelementbyid

Related Related

  1. 1

    Exclude a tag and its content using regex

  2. 2

    Exclude a tag and its content using regex

  3. 3

    Flexbox item height independent of its content

  4. 4

    Python - How to filter a list by the content of its elements?

  5. 5

    How to prevent a flex item from shrinking smaller than its content?

  6. 6

    How to hide a list item according to its html content?

  7. 7

    Using image from sprite in list item after its' content

  8. 8

    Oneline horizontal list with middle item that fits its content width

  9. 9

    How do I exclude a clicked item and all its parents from a jquery selector?

  10. 10

    Exclude item on orderBy

  11. 11

    grep to file to exclude content

  12. 12

    Exclude matched array elements

  13. 13

    Exclude elements from ArrayList

  14. 14

    Exclude multiple elements in .not, including $this

  15. 15

    Exclude children elements

  16. 16

    Exclude multiple elements in .not, including $this

  17. 17

    Exclude children elements

  18. 18

    Cannot add distance between droplist and its content that overlaps some other elements

  19. 19

    How to make up elements which covers the individual cells(which has its own content) of a table in html?

  20. 20

    How to exclude an item in Django loop?

  21. 21

    Copy-Item on registry + exclude

  22. 22

    targeting an element by its content and the content of its ancestors

  23. 23

    How do I use pseudo-elements to display content after the main item?

  24. 24

    Exclude Tags Based on Content in Beautifulsoup

  25. 25

    HTML: content height exclude footer

  26. 26

    Exclude Tags Based on Content in Beautifulsoup

  27. 27

    JQuery selector to exclude child elements

  28. 28

    jQuery: focusout to exclude some elements

  29. 29

    exclude form elements from getelementbyid

HotTag

Archive