Hi
Being tested using FF 3 on WAMP server. I have spent a number of hours trying to figure this out myself.
I have a html form using table cells and had a request to enable the arrow keys on a keyboard to move through the cells. I found some code and adapted it, and it works well now. Arrow keys move the cursor exactly to the correct cell. There are two things I can't do which I was hoping you guys would give me some help on. I am a javascript novice and would be so grateful for some help.
I have posted the code below. My two questions are
1) If I arrow right, and there's some text in the next cell, the cursor lands at the end. How can I make it land at the beginning of the text? (Curiously, if I use IE8, the cursor seems to land at the beginning. I guess I'd like to force it to land at the beginning regardless of browser.)
2) Once the cursor is at the beginning of that cell with text in it, what modification should I make so that when I arrow right, the cursor moves through the text character by character rather than jumping to the next cell?
[CODE=javascript]
<script language="javas cript">
function keyPressed(TB, e)
{
if (e.keyCode == 40 || e.keyCode == 13) { // arrow down
if (TB.split("c")[0] < 6)
document.getEle mentById(eval(T B.split("c")[0] + '+1') + 'c' + TB.split("c")[1]).focus();
if (TB.split("c")[0] == 6)
document.getEle mentById(1 + 'c' + TB.split("c")[1]).focus();
}
if (e.keyCode == 38) { // arrow up
if(TB.split("c" )[0] > 1)
document.getEle mentById(eval(T B.split("c")[0] + '-1') + 'c' + TB.split("c")[1]).focus();
if (TB.split("c")[0] == 1)
document.getEle mentById(6 + 'c' + TB.split("c")[1]).focus();
}
if (e.keyCode == 37) { // arrow left
if(TB.split("c" )[1] > 1)
document.getEle mentById(TB.spl it("c")[0] + 'c' + eval(TB.split(" c")[1] + '-1')).focus();
if (TB.split("c")[1] == 1)
document.getEle mentById(TB.spl it("c")[0] + 'c' + 3).focus();
}
if (e.keyCode == 39) { // arrow right
if(TB.split("c" )[1] < 3)
document.getEle mentById(TB.spl it("c")[0] + 'c' + eval(TB.split(" c")[1] + '+1')).focus();
if (TB.split("c")[1] == 3)
document.getEle mentById(TB.spl it("c")[0] + 'c' + 1).focus();
}
}
</script>[/CODE]
Being tested using FF 3 on WAMP server. I have spent a number of hours trying to figure this out myself.
I have a html form using table cells and had a request to enable the arrow keys on a keyboard to move through the cells. I found some code and adapted it, and it works well now. Arrow keys move the cursor exactly to the correct cell. There are two things I can't do which I was hoping you guys would give me some help on. I am a javascript novice and would be so grateful for some help.
I have posted the code below. My two questions are
1) If I arrow right, and there's some text in the next cell, the cursor lands at the end. How can I make it land at the beginning of the text? (Curiously, if I use IE8, the cursor seems to land at the beginning. I guess I'd like to force it to land at the beginning regardless of browser.)
2) Once the cursor is at the beginning of that cell with text in it, what modification should I make so that when I arrow right, the cursor moves through the text character by character rather than jumping to the next cell?
Code:
<table> <tr> <td><input type='text' id='1c1' value='' onkeydown='keyPressed(this.id,event)'/></td> <td><input type='text' id='1c2' value='' onkeydown='keyPressed(this.id,event)'/></td> <td><input type='text' id='1c3' value='' onkeydown='keyPressed(this.id,event)'/></td> </tr> <tr> <td><input type='text' id='2c1' value='' onkeydown='keyPressed(this.id,event)'/></td> <td><input type='text' id='2c2' value='' onkeydown='keyPressed(this.id,event)'/></td> <td><input type='text' id='2c3' value='' onkeydown='keyPressed(this.id,event)'/></td> </tr> <tr> <td><input type='text' id='3c1' value='' onkeydown='keyPressed(this.id,event)'/></td> <td><input type='text' id='3c2' value='' onkeydown='keyPressed(this.id,event)'/></td> <td><input type='text' id='3c3' value='' onkeydown='keyPressed(this.id,event)'/></td> </tr> <tr> <td><input type='text' id='4c1' value='' onkeydown='keyPressed(this.id,event)'/></td> <td><input type='text' id='4c2' value='' onkeydown='keyPressed(this.id,event)'/></td> <td><input type='text' id='4c3' value='' onkeydown='keyPressed(this.id,event)'/></td> </tr> <tr> <td><input type='text' id='5c1' value='' onkeydown='keyPressed(this.id,event)'/></td> <td><input type='text' id='5c2' value='' onkeydown='keyPressed(this.id,event)'/></td> <td><input type='text' id='5c3' value='' onkeydown='keyPressed(this.id,event)'/></td> </tr> <tr> <td><input type='text' id='6c1' value='' onkeydown='keyPressed(this.id,event)'/></td> <td><input type='text' id='6c2' value='' onkeydown='keyPressed(this.id,event)'/></td> <td><input type='text' id='6c3' value='' onkeydown='keyPressed(this.id,event)'/></td> </tr> </table>
<script language="javas cript">
function keyPressed(TB, e)
{
if (e.keyCode == 40 || e.keyCode == 13) { // arrow down
if (TB.split("c")[0] < 6)
document.getEle mentById(eval(T B.split("c")[0] + '+1') + 'c' + TB.split("c")[1]).focus();
if (TB.split("c")[0] == 6)
document.getEle mentById(1 + 'c' + TB.split("c")[1]).focus();
}
if (e.keyCode == 38) { // arrow up
if(TB.split("c" )[0] > 1)
document.getEle mentById(eval(T B.split("c")[0] + '-1') + 'c' + TB.split("c")[1]).focus();
if (TB.split("c")[0] == 1)
document.getEle mentById(6 + 'c' + TB.split("c")[1]).focus();
}
if (e.keyCode == 37) { // arrow left
if(TB.split("c" )[1] > 1)
document.getEle mentById(TB.spl it("c")[0] + 'c' + eval(TB.split(" c")[1] + '-1')).focus();
if (TB.split("c")[1] == 1)
document.getEle mentById(TB.spl it("c")[0] + 'c' + 3).focus();
}
if (e.keyCode == 39) { // arrow right
if(TB.split("c" )[1] < 3)
document.getEle mentById(TB.spl it("c")[0] + 'c' + eval(TB.split(" c")[1] + '+1')).focus();
if (TB.split("c")[1] == 3)
document.getEle mentById(TB.spl it("c")[0] + 'c' + 1).focus();
}
}
</script>[/CODE]
Comment