joseluisq / printd

Print HTML elements or pages in modern browsers.
MIT License
80 stars 5 forks source link

Fix demo printing currently selected options #20

Closed K-Thompson closed 2 years ago

K-Thompson commented 2 years ago

First, thank you for your very useful utility code.

As you've pointed out if the div contains a select element, Printd prints the default selection rather than what the user has chosen. Your demo has this issue.

To fix, you can make the following 3 changes.

function updateSelectedAttribute(e) {
  let sel, i;

    sel = document.getElementById(e.target.id);
    // remove 'selected' from prior user selection
    for (i = 0; i < sel.length; i += 1) {
      sel[i].removeAttribute("selected");
    }
    // and add 'selected' to current selection
    sel[sel.selectedIndex].setAttribute("selected", "selected");
}
document.getElementById("lang").addEventListener("change", updateSelectedAttribute);

I have tested this in current versions of Chrome, Edge, and Firefox. I have not tested it with any mobile device.

Make these changes and Printd will print what the user has selected. Probably best to have a demo that does what developers expect. ;-)

joseluisq commented 2 years ago

Thanks for that! The demo should be fixed now.