To alter the class based on whether a field is empty or not, you can utilize CSS @media in conjunction with JavaScript. Below is an example of how this can be achieved.
var fields = document.getElementsByClassName("field");
for(var i=0; i < fields.length; i++){
fields[i].addEventListener('keyup', function() {
if(this.value.length) {
this.parentElement.className = "";
} else {
this.parentElement.className = "empty";
}
});
}
@media print {
.empty {
display: none;
}
}
<div class="empty">Name: <input class="field"></div>
<div class="empty">Field: <input class="field"></div>
<div class="empty">Foo: <input class="field"></div>
(Try adding content to a field but leave some empty before pressing ctrl+p. The empty fields should not appear in the print preview)
If jQuery is being used, the selectors and looping in the JavaScript code can be simplified as shown below:
$(".field").on("keyup", function () {
$this = $(this);
if ($this.val().length) {
$this.parent().removeClass("empty");
} else {
$this.parent().addClass("empty");
}
});