Change caratPos variable into use element value length instead of cursor position