简体   繁体   中英

How to hide div from printing only for prints over a specific link tag with js script?

The page has to have two print versions. One with price and on without. My solution works in Firefox, but not in chromebased Browser.

What I do: If the printlink is klicked, I add the print:hidden-class name, then print, then remove the class:

<a href="javascript:if(window.print){document.getElementById('priceinfo').classList.add('print:hidden'); window.print(); document.getElementById('priceinfo').classList.remove('print:hidden');} ">print without price</a>

Why ist this not working in Edge or Chrome? And what will work?

Thanks @Fractalism moving the code into script tags worked:

<script>
  document.querySelector('#print-no-price').addEventListener('click', () => {
    if(window.print) {
      document.getElementById('priceinfo').classList.add('print:hidden'); 
      window.print(); 
      document.getElementById('priceinfo').classList.remove('print:hidden');
    }
  });
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM