Javascript

Input box 자동으로 cursor 위치 이동

pipinstall 2021. 2. 19. 17:22

$.fn.setCursorPosition = function( pos )

{

  this.each( function( index, elem ) {

    if( elem.setSelectionRange ) {

      elem.setSelectionRange(pos, pos);

    } else if( elem.createTextRange ) {

      var range = elem.createTextRange();

      range.collapse(true);

      range.moveEnd('character', pos);

      range.moveStart('character', pos);

      range.select();

    }

  });

 

  return this;

};

 

 

  3번째  indexd에 위치

$("#test").focus().setCurserPosition(3);


 

<html>

 <body>

    <div>

        <input type="text" id="test"/> 

    </div>

 

</body>

</html>

 

'Javascript' 카테고리의 다른 글

배열  (0) 2021.02.18