joseluisq/printd

Fix demo printing currently selected options

K-Thompson opened this issue · 1 comments

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.

  • Add id to select: id="lang"
  • Add this generic function to the end of your JavaScript.
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");
}
  • Add the change event listener.
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. ;-)

Thanks for that!
The demo should be fixed now.