I'm trying to implement a simple Javascript that will cause the focus to jump to the next input box with a particular CSS class when the "period" or "slash" (.
/
) keys are pressed.
The input boxes with the class userinput
are the 20 boxes underneath the "IP Address" column on the following page, to the left of the Check/Show buttons.
http://www.practicalnetworking.net/subnet2.html
Here is the script I added:
<script type="text/javascript">
// Jump to next box on keypress: period
$(window).load(function(){
var currentBoxNumber = 0;
$(".userinput").keyup(function (event) {
if (event.keyCode == 190) {
textboxes = $("input.userinput");
currentBoxNumber = textboxes.index(this);
console.log(textboxes.index(this));
if (textboxes[currentBoxNumber + 1] != null) {
nextBox = textboxes[currentBoxNumber + 1];
nextBox.focus();
nextBox.select();
event.preventDefault();
return false;
}
}
});
});
</script>
I got the script from this JFiddle, and it works perfectly fine in the JFiddle:
I got a keycode of 190 for .
on this site:
http://www.asquare.net/javascript/tests/KeyCode.html
I got 191 for /
, but haven't added that into the code yet. Still trying to get it to work with the .
.
Disclaimer, I am a javascript newb. So please consider explaining the solution like I'm 5 =). Thank you.