From 5b1457835be6c00a7d108537ecc88d3d85d48d51 Mon Sep 17 00:00:00 2001 From: n-peugnet Date: Tue, 28 Apr 2020 18:08:35 +0200 Subject: feat: select all for code inputs This enable the "select all" feature for `input.select-all` element. input elements are used because these are the only one with the `select()` method. To obtain the same rendering as before the readonly attribute and the class `code` have been added to these elements. @vincent-peugnet: make sure to use this element for future select-all Closes #107 --- src/fn/fn.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'src/fn/fn.js') diff --git a/src/fn/fn.js b/src/fn/fn.js index bca4eaf..a5aa538 100644 --- a/src/fn/fn.js +++ b/src/fn/fn.js @@ -28,3 +28,24 @@ export function closeSubmenus(e) { } } } + +/** + * Select the whole content of the clicked item. + * @param {MouseEvent} e + */ +function selectAll(e) { + if (e.target instanceof HTMLInputElement) { + e.target.select(); + e.target.focus(); + } +} + +/** + * Activate "select all" feature for `input.select-all` elements. + */ +export function activateSelectAll() { + let selectAllInputs = document.querySelectorAll('input.select-all'); + for (const selectAllInput of selectAllInputs) { + selectAllInput.addEventListener('click', selectAll); + } +} -- cgit v1.2.3