Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IE9 multiple select overflow while printing

I'm having problems with IE9 ignoring the select borders when printing a multiple select.

Here's how to recreate the problem:

  1. Open IE9 on Windows 7.
  2. Go to w3schools's multiple select edit page.
  3. Now highlight the options and copy/paste until there is a long list of duplicates.
  4. Then remove the size attribute.
  5. Click on "Edit and Click Me" so that the page reloads and you now have your modified select in the second panel.
  6. Now, print the document (even using the XPS viewer).

For me, all of the options are printed on the page, even though the select is only 4 option elements tall. This still happens to some degree if you leave the "size" attribute at the default value of 2, but it's far more obvious when it is changed or removed.

Is anyone else experiencing this? Is this an IE bug? Does anyone know of a workaround?

like image 629
spizzat2 Avatar asked Jan 23 '26 03:01

spizzat2


2 Answers

You can work around this by viewing the site in IE9's compatibility mode. Usually IE will determine that it cannot display a site properly and give you the option to turn on compatibility mode from within the address bar but sometimes you need to explicitly set it.

How to turn on compatibility mode - http://www.sevenforums.com/tutorials/1196-internet-explorer-compatibility-view-turn-off.html - I used the first one in method 2.

like image 128
Tim Fletcher Avatar answered Jan 24 '26 17:01

Tim Fletcher


There doesn't seem to be any CSS solution for this. Instead, I wrote a small jQuery script that copies the <select multiple> contents into a <div>, so that it can be printed. Then I applied some CSS to make it look like a select, and only show the copy when actually printing.

Script:

//jQuery required
$(function() {
  if(!$.browser.msie) return false;
  $('select[multiple]').each(function() {
    $lPrintableDiv = $('<div data-for="' + this.id + '" />').addClass($(this).attr('class')).addClass('printable');

    //update printable on changes
    $(this).after($lPrintableDiv).change(function($aEvent){
      updatePrintable($aEvent.target);
    });

    //run once on load
    updatePrintable(this);
  });
});

function updatePrintable($aTarget) {
  var $lSelect = $($aTarget);
  var $lSelected = $($aTarget).val();
  var $lPrintable = $('[data-for="'+$aTarget.id+'"]');

  $($lPrintable).width($lSelect.width()).height($lSelect.height());
  $($lPrintable).html('');

  $($aTarget).children().each(function($lElm){
    $lVal = $(this).val();
    $lLabel = $('<label />').text($lVal);
    $lOption = $('<input type="checkbox" />').val($lVal);

    if($(this).is(':selected'))
    $lOption.prop('checked', true);

    $lPrintable.append($lOption).append($lLabel);
  });
}

CSS:

.printable {
    border: 1px solid grey;
    display: none;
    overflow: auto;
}

    .printable label {
        display: block;
        font: .8em sans-serif;
        overflow: hidden;
        white-space: nowrap;
    }

    .printable [type="checkbox"] {
        display: none;
    }

    .printable [type="checkbox"]:checked + label {
        background: #3399ff;
        color: white;
    }

@media print {
    select[multiple] { display: none; }
    .printable { display: inline-block; }
    .printable [type="checkbox"]:checked + label { background: grey; }
}

Also see the jsFiddle and original post about this fix

like image 32
Stephan Muller Avatar answered Jan 24 '26 17:01

Stephan Muller



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!