function getInputSelection (el) {
  let startIndex = 0; let endIndex = 0; let normalizedValue; let range; let textInputRange; let len; let endRange

  if (typeof el.selectionStart === 'number' && typeof el.selectionEnd === 'number') {
    startIndex = el.selectionStart
    endIndex = el.selectionEnd
  } else {
    range = document.selection.createRange()

    if (range && range.parentElement() === el) {
      len = el.value.length
      normalizedValue = el.value.replace(/\r\n/g, '\n')

      // Create a working TextRange that lives only in the input
      textInputRange = el.createTextRange()
      textInputRange.moveToBookmark(range.getBookmark())

      // Check if the startIndex and endIndex of the selection are at the very end
      // of the input, since moveStart/moveEnd doesn't return what we want
      // in those cases
      endRange = el.createTextRange()
      endRange.collapse(false)

      if (textInputRange.compareEndPoints('StartToEnd', endRange) > -1) {
        startIndex = endIndex = len
      } else {
        startIndex = -textInputRange.moveStart('character', -len)
        startIndex += normalizedValue.slice(0, startIndex).split('\n').length - 1

        if (textInputRange.compareEndPoints('EndToEnd', endRange) > -1) {
          endIndex = len
        } else {
          endIndex = -textInputRange.moveEnd('character', -len)
          endIndex += normalizedValue.slice(0, endIndex).split('\n').length - 1
        }
      }
    }
  }

  return {
    startIndex,
    endIndex,
    start: _positionForIndex(startIndex),
    end: _positionForIndex(endIndex)
  }

  /**
   * Returns textarea row and column position for certain index
   * @param {Number} index text index
   * @returns {{row: Number, column: Number}}
   */
  function _positionForIndex (index) {
    const textTillIndex = el.value.substring(0, index)
    const row = (textTillIndex.match(/\n/g) || []).length + 1
    const col = textTillIndex.length - textTillIndex.lastIndexOf('\n')

    return {
      row,
      column: col
    }
  }
}
Return an object with the selection range or cursor position (if both have the same value)
Support also old browsers (IE8-)
Source: http://ourcodeworld.com/articles/read/282/how-to-get-the-current-cursor-position-and-selection-within-a-text-input-or-textarea-in-javascript
@param {DOMElement} el A dom element of a textarea or input text.
@return {Object} reference Object with 2 properties (start and end) with the identifier of the location of the cursor and selected text.