how to select text in input javascript code example

Example 1: select text with javascript

// highlight text in an input element
element.select();

// highlight a portion of an element
// element.setSelectionRange(start, end, ?direction)
// start, end - start and end indices of the new selection
// direction (optional) - "forward", "backward", or "none" (default)
element.setSelectionRange(3, 5);

Example 2: js select input contents

function selectText() {
  const input = document.getElementById('text-box');
  input.focus();
  input.select();
}

Example 3: javascript select input text on focus

focusMethod = function getFocus() {           
  document.getElementById("myTextField").focus(); //select the input textfield and set the focus on it
}