I have a jsp page, i want to implement up-down-left-right key functionality into page. i have following code but it visits the readonly text box as well. i want to skip that readonly inputs. Please check attached snap. I have A,B,C,D,E,F,G,H inputs but E and F are the readonly. My cursor is on C. suppose if i press down key (code=40) then it should go to G by skipping E and F.Same with other as well. Please check this link for image:
https://www.dropbox.com/s/ptm483avp8pm9sg/Untitled.png?dl=0
$(document).ready(function(){
$("input,textarea").keydown(function(e) {
if (e.keyCode==37 || e.keyCode==38 ) {
$(":input,textarea,select")[$(":input,select").index(document.activeElement) - 1].focus();
}
if (e.keyCode==39 || e.keyCode==40 ) {
$(":input,textarea,select")[$(":input,select").index(document.activeElement) + 1 ].focus();
}
});
});
You can use the :not([readonly])
selector. E.g:
$(document).ready(function(){
$("input,textarea").keydown(function(e) {
if (e.keyCode==37 || e.keyCode==38 ) {
$(":input:not([readonly]),textarea,select")[$(":input:not([readonly]),select").index(document.activeElement) - 1].focus();
}
if (e.keyCode==39 || e.keyCode==40 ) {
$(":input:not([readonly]),textarea,select")[$(":input:not([readonly]),select").index(document.activeElement) + 1 ].focus();
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='text' value='A' />
<input type='text' value='B' />
<input type='text' value='C' />
<input type='text' value='D' />
<input type='text' value='E' readonly />
<input type='text' value='F' readonly />
<input type='text' value='G' />
<input type='text' value='H' />