CSS might be able to do this, but I'm thinking not as you say section(s), which implies headings of some sort like label elements or just div or p that tell you what section it is. If not, you can set empty elements to display none (once again, in the @media print section - you might want to also use a parent selector if only certain ones of the empty inputs should not be printed):
Code:
input[value=''] {display: none;}
But if there are, as I say labels or other demarcating elements, text, just instructions, whatever like that in the sections that you don't want printed, then you would need javascript.
You may even need to add some elements. And I'm wondering if your page already uses jQuery, as that would make coding it easier. In any case I would want to see the page before making specific recommendations.
That said, generally what you would do is make up your own print function that uses window.print() after checking and making sure things are how you want. From what you say, let's say each section you want has a div around it with an id. Sections 2 and 3 are "section2" and "section3", so, the code could look like so:
Code:
function myPrint(){
var s2 = document.getElementById('section2'), s3 = document.getElementById('section3'),
i2 = s2.getElementsByTagName('input')[0].value, i3 = s3.getElementsByTagName('input')[0].value,
r = /^\s+|\s+$/g, t = /[\D\d]{3,}/; // r strips leading and trailing white space, t tests for 3 or more remaining ordinary characters
s2.style.display = t.test(i2.replace(r, ''))? '' : 'none';
s3.style.display = t.test(i3.replace(r, ''))? '' : 'none';
window.print();
}
This assumes you are using the button to print the page as I mentioned before. Now, with this code accessible to that button, change it to:
Code:
<input type="button" value="Print" onclick="myPrint()">
That may be less than ideal though, because after that, those sections will no longer be able to be seen until the page is refreshed. So, instead of determining what display property should be used, we may want to activate various alternate print stylesheets upon the basis of this information. That would allow a person to go back later, fill in sections 2 and/or 3 and then print and have that work out.
At the same time, requiring a refresh before that could happen might be a good thing.
If you want more help, please post a link to the page on your site that contains the problematic code.
Bookmarks