Thursday, February 14, 2019

Changing Styles for print media query

I had a situation where I needed to change what would be printed based on what button on the web page the user clicked. Here is an easy way to do that.



<style type="text/css" media="print" id="printStyles">
</style>

<script>
    function printOptionA() {
        var styles = "#optionA {display: block;} #optionB {display:none;}";
        $('#printStyles').text(styles);
        window.print();
    }

 function printOptionB() {
        var styles = "#optionB {display: block;} #optionA {display:none;}";
        $('#printStyles').text(styles);
        window.print();
    }
</script>

The two functions can be called from button, hyperlinks, etc.

If the user just does a Control-P to print in the browser it will print the page as expected (neither of these changes). There is no need to undo these changes after printing.

6 comments:

prinfo12 said...

good information thanks for sharing
Best SEO and Digital Marketing Agency/company in Bangalore!


Best SEO and Digital Marketing Agency/company in Bangalore!


Best SEO and Digital Marketing Agency/company in Bangalore!


Best SEO and Digital Marketing Agency/company in Bangalore!


Best SEO and Digital Marketing Agency/company in Bangalore!


Best SEO and Digital Marketing Agency/company in Bangalore!


Best SEO and Digital Marketing Agency/company in Bangalore!

Edit Point India said...

Thanks for the amazing information. For Best Edius X Pro Price In India.
Visit : editpointindia.com

salesforce cpq course said...

Mulesoft certificationmulesoft certification training training helps learners prepare effectively for industry-recognized exams.

vr said...

Fantastic article! Our database modeling course
equips professionals to structure data, design schemas, and improve database efficiency. Hands-on learning ensures skills you can apply immediately.

anjani02 said...

I found this article really interesting and educational. The explanations are simple and clear. Looking forward to reading more posts from you.online ba analyst course

Sk said...

This aws devops course helps learners understand cloud and deployment concepts through simple and practical learning sessions.