Select all in contenteditable focus

http://stackoverflow.com/q/34950616/145346

by amrendra kumar

HTML

<span id="editable" contenteditable="true">This is a test</span>

JavaScript

var input = document.getElementById("editable");

input.addEventListener('focus', function(){
  this.classList.add('is-focused');
});

input.addEventListener('blur', function(){
  this.classList.remove('is-focused');
});

const setCaretPos = (input_node, pos) => {
  if (input_node.setSelectionRange) {
  console.log('check 1')
    input_node.setSelectionRange(pos, pos)
  } else if (input_node.createTextRange) {
    input_node.createTextRange().move('character', pos)
    console.log('check 2')
  }
  input_node.focus()
}

console.log('foind input', input, input.textContent.length)
  
  
  
  window.onload = () => {
  console.log('called')
  setCaretPos(input, input.textContent.length-1 )
  //input.focus()
  }