5

I have an html select element with an id of background designed to set a background image for the <div class=”results-area”>. Then I have a print button to print the .results-area content.

The problem is that when I wanna print the .results-area I need to set what background image to set when printing, if I use CSS @media print I cannot set rules which background it should print (based on user selection).

Is there another way to accomplish that?

HTML

<select id="background">
<option value="url(picture1.jpg)">picture1</option>
<option value="url(picture2.jpg)">picture2</option>
<option value="url(picture3.jpg)">picture3</option>
<option value="url(picture4.jpg)">picture4</option>
<option value="url(picture5.jpg)">picture5</option>
<option value="url(picture6.jpg)">picture6</option>
</select>

<div class=”results-area”>
...
</div>

<a id="printMe" class="btn btn-warning">Print</a>

jQuery

$('#background').change(function () {
    $('.results-area').css("background-image", $(this).val());
})


$('#printMe').click(function () {
 print() 
});

CSS:

@media print {
          .results-area{
              background-image: ??????;
          }
    }
Ahron M. Galitzky
  • 2,219
  • 2
  • 13
  • 25
  • look at http://stackoverflow.com/questions/512054/setting-background-image-using-jquery-css-property – Bindrid Dec 01 '16 at 19:47
  • 1
    Don't forget that on many default browser settings, background image printing is turned off. So this wouldn't print for many users no matter what. – andi Dec 01 '16 at 20:01

3 Answers3

3

The way I solved it is as follows.

HTML

I changed the values to digits "1-2-3..." And added a class to each picture "a-b-c..."

<select id="background">
   <option class="a" value="1">Picture 1</option>
   <option class="b" value="2">Picture 2</option>
   <option class="c" value="3">Picture 3</option>
   <option class="d" value="4">Picture 4</option>
   <option class="e" value="5">Picture 5</option>
   <option class="f" value="6">Picture 6</option>
</select>

<div class=”results-area”>
...
</div>

<a id="printMe" class="btn btn-warning">Print</a>

CSS

With the @media print I specified for each class his own background-image

@media print {
   .a {
     background-image: url(image1.jpg) !important;
   }

   .b {
     background-image: url(image2.jpg) !important;
   }

   .c{
     background-image: url(image3.jpg) !important;
   }

   .d{
     background-image: url(image4.jpg) !important;
   }

   .e {
     background-image: url(image5.jpg) !important;
   }

   .f {
     background-image: url(image6.jpg) !important;
   }
}

JQuery

$('#background').change(function () {
    if ($(this).val() === "1") {
    $('.results-area').css("background-image", "url(image1.jpg)");
    replace();
    $('.results-area').addClass("a");
}
else if ($(this).val() === "2") {
    $('.results-area').css("background-image", "url(image2.jpg)");
    replace();
    $('.results-area').addClass("b");
}
else if ($(this).val() === "3") {
    $('.results-area').css("background-image", "url(image3.jpg)");
    replace();
    $('.results-area').addClass("c");
}
else if ($(this).val() === "4") {
    $('.results-area').css("background-image", "url(image4.jpg)");
    replace();
    $('.results-area').addClass("d");
}
else if ($(this).val() === "5") {
    $('.results-area').css("background-image", "url(image5.jpg)");
    replace();
    $('.results-area').addClass("e");
}
else if ($(this).val() === "6") {
    $('.results-area').css("background-image", "url(image6.jpg)");
    replace();
    $('.results-area').addClass("f");
}
else {
    replace();
}
});


function replace() {
    $('.results-area').removeClass("a b c d e f");
};

$('#printMe').click(function () {

        print()
});

It feels like a crazy way of doing it, but it did the job.

I hope this works for you too!

Ahron M. Galitzky
  • 2,219
  • 2
  • 13
  • 25
2

Add a <style> tag to your <head> and give it an id:

<head>
...
<style id="bg-for-print"></style>
...
</head>

Then when you want to change the background image, select that style tag and set the appropriate background image:

$("#bg-for-print").text(`
    @media print {
        .results-area{
            background-image: url(${backgroundUrl});
        }
    }
`);
cooljeffro
  • 626
  • 7
  • 5
0

Set background image using jquery and set the background-size:0; in default and background-size: inherit; in print media

$('select').on('change', function() {
        $('.results-area').css("background-image",'url(\''+this.value+'\')');
    });
$('#printMe').click(function () {
        window.print();
    });

        $('.results-area').css("background-image",'url(\''+$('select')[0].value+'\')');
.results-area{
  width:225px;
  height:225px;
  background-size: 0;
  border:solid 1px red;
}
 @media print {
          .results-area{
  background-size: inherit;
          }
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="selImage">
  <option value="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcTBoO6az8tPqavBo9xddKQynhqR8YSt4MUWTouGdFp7bWfxJiyW" selected>bg1</option>
  <option value="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcSIo-K6XoQSaPZp_MP_TD4OeIdu229F4TCnZARtoiWvHJmI3iy8">bg2</option>
  
</select>
<input type="button" id="printMe" value="print"/>
<div class="results-area">
  </div>
jafarbtech
  • 6,842
  • 1
  • 36
  • 55