본문 바로가기

Story/Javascript

form 안에 text box 를 화살표키로 이동하도록...

반응형
<script language="JavaScript">
document.onkeydown = checkKeycode
function checkKeycode(e) {
    var keycode;
    var tar = event.srcElement.form;
    var Tindex = "";
    if (window.event && window.event.keyCode == 40){
        for(i=0;i<tar.elements.length;i++){
            if(tar.elements[i] == event.srcElement) Tindex = i;
            if(Tindex !== i && Tindex !== "" && tar.elements[i].type == "text"){
                tar.elements[i].focus();
                break;
            }
        }
    }else if (window.event && window.event.keyCode == 38){
        for(i= (tar.elements.length - 1);i>=0;i--){
            if(tar.elements[i] == event.srcElement) Tindex = i;
            if(Tindex !== i && Tindex !== "" && tar.elements[i].type == "text"){
                tar.elements[i].focus();
                break;
            }
        }
    }
}
</script>
반응형