Skip to content

Instantly share code, notes, and snippets.

@wiseoldman
Last active May 4, 2018 08:53
Show Gist options
  • Save wiseoldman/e24f018c2406ed754c65658acfdb46a3 to your computer and use it in GitHub Desktop.
Save wiseoldman/e24f018c2406ed754c65658acfdb46a3 to your computer and use it in GitHub Desktop.
[PrintButton] Small snippet for creating a print button
class PrintButton {
constructor(PRINT_CLASS = '.print') {
this.PRINT_CLASS = PRINT_CLASS;
this.PRINT_TARGETS = document.querySelectorAll(this.PRINT_CLASS);
if (this.PRINT_TARGETS.length) {
this.addPrintListener();
}
}
addPrintListener() {
for (let i = 0; i < this.PRINT_TARGETS.length; i++) {
const TARGET = this.PRINT_TARGETS[i];
TARGET.addEventListener('click', (e) => {
e.preventDefault();
window.print();
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment