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? - <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="javascript">
-
-
function keyPressed(TB, e)
-
{
-
if (e.keyCode == 40 || e.keyCode == 13) { // arrow down
-
if (TB.split("c")[0] < 6)
-
document.getElementById(eval(TB.split("c")[0] + '+1') + 'c' + TB.split("c")[1]).focus();
-
if (TB.split("c")[0] == 6)
-
document.getElementById(1 + 'c' + TB.split("c")[1]).focus();
-
}
-
-
if (e.keyCode == 38) { // arrow up
-
if(TB.split("c")[0] > 1)
-
document.getElementById(eval(TB.split("c")[0] + '-1') + 'c' + TB.split("c")[1]).focus();
-
if (TB.split("c")[0] == 1)
-
document.getElementById(6 + 'c' + TB.split("c")[1]).focus();
-
}
-
-
if (e.keyCode == 37) { // arrow left
-
if(TB.split("c")[1] > 1)
-
document.getElementById(TB.split("c")[0] + 'c' + eval(TB.split("c")[1] + '-1')).focus();
-
if (TB.split("c")[1] == 1)
-
document.getElementById(TB.split("c")[0] + 'c' + 3).focus();
-
}
-
-
if (e.keyCode == 39) { // arrow right
-
if(TB.split("c")[1] < 3)
-
document.getElementById(TB.split("c")[0] + 'c' + eval(TB.split("c")[1] + '+1')).focus();
-
if (TB.split("c")[1] == 3)
-
document.getElementById(TB.split("c")[0] + 'c' + 1).focus();
-
}
-
}
-
-
</script>
4 6242
Hi,
I did some changes I found it to be working. Please post back if you have any problem in that
javascript :
HTML 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>
Thanks and Regards
Ramanan Kalirajan
Hi RamananKalirajan
Thanks for your work on this. It's fantastic. It now works perfectly in IE. However, it doesn't work in FF, Safari, Chrome or Opera. I should note that the up/down arrowing works fine. It's the left/right arrowing that is the problem. Obviously I'd like to have it working in them too, or it won't be viable. I've included the error message for each browser below. If you know of a way to fix these I'd appreciate it.
FF3.5.2: document.selection is undefined
Chrome 2.0: cannot call method 'CreateRange' of undefined
Safari: Result of expression 'document.selection' [undefined] is not an object
Opera: Statement on line 9: Type mismatch (usually non-object value supplied where object required)
Backtrace:
Line 9 of inline#1 script in http://localhost/test3.php: In function GetCursorPosition
while (cur.compareEndPoints("StartToStart", tr) > 0) {
Line 98 of inline#1 script in http://localhost/test3.php: In function keyPressed
if(0!=GetCursorPosition())
Line 1 of function script
keyPressed(this.id,event)
The cursor position code is incorrect. See ths thread for a better script.
Thanks Acoder. That link was really awesome. I got the solution for detecting cursor position in all the browsers. Thanks again
Thanks and Regards
Ramanan Kalirajan
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Darren Oakey |
last post by:
ok - the problem - I made a simple breakout game out of a form, just
painting the background - and using keydown for left and right arrow keys to
control the bat - worked fine.
I then moved all...
|
by: Neil Wallace |
last post by:
Hi there,
I have an application in which a grid of 100 or more buttons are put on a
form in columns of 10.
All the buttons are within a panel.
They are added in runtime, and so they adopt a...
|
by: Rlrcstr |
last post by:
How can you detect when an arrow key gets pressed? Doesn't seem to trigger
a KeyPress or KeyDown event. Thanks.
Jerry
|
by: Phil Galey |
last post by:
I have a Panel control docked on all sides on a form and the panel control
contains a PictureBox. I'm using the KeyDown event of the form to respond to
the and keys for resizing the image and the...
|
by: jonefer |
last post by:
I tried all sorts of key combinations.
How do I precisely move controls, such as labels or textboxes with the arrow
keys?
I notice that new labels or boxes only have 2 handles on them, -...
|
by: Vincent |
last post by:
Hi,
I have a user control that needs to trap the arrow keys to move items
around internally. However, using the arrow keys will move the focus to
another control on the form hosting the user...
|
by: Martijn Mulder |
last post by:
/*
I have problems detecting the Arrow Keys on a User Control. A control
derived from System.Windows.Forms.Control neglects 'bare' Arrow Keys
but does react on the combination <Altor <Ctrl+ Arrow...
|
by: Martijn Mulder |
last post by:
/*
I override IsInputKey() to direct the Arrow Keys (Cursor Keys) to my
custom System.Windows.Forms.Control. But, holding down the Shift-Key
prevents the Arrow Keys from coming through.
How can...
|
by: boopsboops |
last post by:
Hi thescripts people, I hope I'm in the right forum for Visual Basic Dotnet (VS 2005).
I am trying to make a custom control in which you can nudge a point around using the arrow keys. Actually,...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |