Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was created one website, which contain an image (like a table), and I want simulate input over this picture. So, I define input boxes, each having a maximum length = 1. The first input box contains the autofocus option. I would like that after pressing a character in first input box, the focus to be set in the second input box, and so on.
I assume that it is therefore necessary one key down function, which contains the names of boxes, but I do not know how to continue this.
Could someone help me what instructions should be use in the function?
Thank you very much in advance.
Here is my code.
HTML
<p><img alt="" src="http://files.....jpg" style="width: 500px; height: 783px;"></p>
<style type="text/css">
#Editbox1
{
border: 1px #A9A9A9 none; background-color: transparent; color :#000000;font-family:
Times New Roman; font-weight:bold;font-size: 30px; text-align: center;vertical-align: middle;
}#Editbox2
{
border: 1px #A9A9A9 none; background-color: transparent; color :#000000;font-family:
Times New Roman; font-weight:bold;font-size: 30px; text-align: center;vertical-align: middle;
}#Editbox3
{
border: 1px #A9A9A9 none; background-color: transparent; color :#000000;font-family:
Times New Roman; font-weight:bold;font-size: 30px; text-align: center;vertical-align: middle;
}
</style>
<p><input autofocus="" id="Editbox1" maxlength="1" name="Editbox1"  onkeydown="onkeydownfunction(event)"  style="position:absolute; left:260px; top:303px;width:40px;height:40px;line-height:20px;z-index:1;" type="text" value="">
</p>
<p><input  id="Editbox2" maxlength="1" name="Editbox2" onkeydown="onkeydownfunction(event)"  style="position:absolute; left:315px; top:303px;width:40px;height:40px;line-height:20px;z-index:1;" type="text" value="">
</p>
<p><input id="Editbox3" maxlength="1" name="Editbox3" onkeydown="onkeydownfunction(event)"  style="position:absolute; left:370px; top:303px;width:40px;height:40px;line-height:20px;z-index:1;" type="text" value="">
</p>

function keydownfunction(event)
{
var tab=new Array("Editbox1","Editbox2","Editbox3");
.
.
.

}
Posted

1 solution

Here I am assuming that you have only three textbox which is not generating dynamically.

here is the html code :

JavaScript
<input type="text" id="Editbox1" onkeypress="MoveToNextBox(this, event)"  maxlength="1" />
        <input type="text" id="Editbox2" onkeypress="MoveToNextBox(this, event)"  maxlength="1" />
         <input type="text" id="Editbox3"  maxlength="1" />


here is javascript code :

JavaScript
function MoveToNextBox(obj, event) {
   var tab=new Array("Editbox1","Editbox2","Editbox3");
   var id = obj.id;
   var backSpace = event.keyCode; //find keypress
   if (backSpace != 8) { //check if backspace is pressed or not
       if (id == tab[0]) { //match the id
           document.getElementById(tab[1]).focus(); //set focus to next textbox
       }
       if (id == tab[1]) {
           document.getElementById(tab[2]).focus();
       }
   }
}


Good luck.
 
Share this answer
 
v3
Comments
Member 10565876 24-Nov-15 10:00am    
Thank you very match! The code is very good. I’ve one another question. In the same function can be tested, that the typed character is between 1-9?
Raje_ 24-Nov-15 10:07am    
Yes you can check use regular expression to check typed character :

var regex=/^[1-9]+$/;
if (x.match(regex))
{
if (id == tab[0]) { //match the id
document.getElementById(tab[1]).focus(); //set focus to next textbox
}
if (id == tab[1]) {
document.getElementById(tab[2]).focus();
}
}
else{
alert("Must input numbers");
return false;
}
Member 10565876 24-Nov-15 10:43am    
Ok! Excuse me, but who is x?
Raje_ 25-Nov-15 3:58am    
x will be your current value, var x = obj.value; You should not expect everything from us. You should do little home work by your self too. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900