473,406 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Cancel the character to be written?

CK
The follwing function is in an onkeypress event for some textboxes. How can
I cancel the keystroke? or more importantly it's output. This function does
what I want it to do, but it actually writes a space in the textbox. I
wanted to escape out of writing the character. Especially if I change it to
another keyCode besides space bar. Any ideas?

Thanks In Advance,
~CK

function setEfforts(ctrl){
if(event.shiftKey && event.keyCode == 32)
{
var row = ctrl.parentNode.parentNode;
var inputs = row.getElementsByTagName('input');
var textboxes = new Array();
for(var x = 0; x < inputs.length; x++)
if(inputs[x].type == 'text'){textboxes.push(inputs[x]);}

for(var y = 0; y < textboxes.length; y++)
textboxes[y].value = ctrl.value;
}
return false;

}
Sep 21 '06 #1
2 3798
on your event handler you must say return setEfforts(event)
<input type="text" onclick="return setEfforts(event)" />
CK wrote:
The follwing function is in an onkeypress event for some textboxes. How can
I cancel the keystroke? or more importantly it's output. This function does
what I want it to do, but it actually writes a space in the textbox. I
wanted to escape out of writing the character. Especially if I change it to
another keyCode besides space bar. Any ideas?

Thanks In Advance,
~CK

function setEfforts(ctrl){
if(event.shiftKey && event.keyCode == 32)
{
var row = ctrl.parentNode.parentNode;
var inputs = row.getElementsByTagName('input');
var textboxes = new Array();
for(var x = 0; x < inputs.length; x++)
if(inputs[x].type == 'text'){textboxes.push(inputs[x]);}

for(var y = 0; y < textboxes.length; y++)
textboxes[y].value = ctrl.value;
}
return false;

}
Sep 22 '06 #2

CK wrote:
The follwing function is in an onkeypress event for some textboxes. How can
I cancel the keystroke? or more importantly it's output. This function does
what I want it to do, but it actually writes a space in the textbox. I
wanted to escape out of writing the character. Especially if I change it to
another keyCode besides space bar. Any ideas?

Thanks In Advance,
~CK

function setEfforts(ctrl){
if(event.shiftKey && event.keyCode == 32)
You seem to be assuming IE, this will not work in other browsers, try
the following with the modified call below:

function setEfforts(e, ctrl) {
if(e.shiftKey && (e.keyCode == 32 || e.which == 32)) {
{
var row = ctrl.parentNode.parentNode;
var inputs = row.getElementsByTagName('input');
var textboxes = new Array();
for(var x = 0; x < inputs.length; x++)
if(inputs[x].type == 'text'){textboxes.push(inputs[x]);}

for(var y = 0; y < textboxes.length; y++)
textboxes[y].value = ctrl.value;
}
It seems rather pointless to iterate over the inputs collection and put
the text inputs into an array just so you can iterate over them again.
Why not do the processing the first time around?

var row = ctrl.parentNode.parentNode;
var inputs = row.getElementsByTagName('input');
var input, i=inputs.length;

while (i--){
input = inputs[i];
if(input.type == 'text'){
input.value = ctrl.value;
}
}
return false;
}
In the call from the event handler, pass the event object to the
function with a reference to the element and return the return value of
the function to the event handler, so you call it with:

<input ... onkeypress="return setEfforts(event, this);">

and it works in a much wider selection of browsers. There's a working
example below:

<script type="text/javascript">

function setEfforts(e, ctrl){
if(e.shiftKey && (e.keyCode == 32 || e.which == 32)) {
var row = ctrl.parentNode.parentNode;
var inputs = row.getElementsByTagName('input');
var input, i=inputs.length;

while (i--){
input = inputs[i];
if(input.type == 'text'){
input.value = ctrl.value;
}
}
return false;
}
}

</script>

<form action="">
<table>
<tr>
<td><input type="text"
onkeypress="return setEfforts(event, this);">
<td><input type="text"
onkeypress="return setEfforts(event, this);">
<td><input type="text"
onkeypress="return setEfforts(event, this);">
</tr>
<tr><td colspan="3"><input type="reset">
</tr>
</table>
</form>
--
Rob

Sep 22 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mike - EMAIL IGNORED | last post by:
Using RoseRT with RedHat 8.0, I have a thread that is waiting for a cin, and another thread that wants to cancel the cin without waiting for a response from the keyboard. I tried destroying the...
3
by: Jacob | last post by:
I'm writing a class that communicates with a server using the TcpClient class. Most of the methods I've written are intended to be used synchronously and will block until they are completed. But...
14
by: clintonG | last post by:
This is an appeal for peer support sent to Microsoft as will be noted in closing. The Login control does not include a Cancel button. The only option is to convert the Login control to a...
1
by: sonald | last post by:
Dear All, I am working on a module that validates the provided CSV data in a text format, which must be in a predefined format. We check for the : 1. Number of fields provided in the text file,...
44
by: Kulgan | last post by:
Hi I am struggling to find definitive information on how IE 5.5, 6 and 7 handle character input (I am happy with the display of text). I have two main questions: 1. Does IE automaticall...
4
by: martin | last post by:
Hello, Is there a way to make a kind of "cancel" button on a form? Suppose you accidently changed or overwrote some data in a form, then I'd like to leave this form at once and cancel any...
5
by: Timothy Madden | last post by:
Hello Is there a function that will allow me to output text written in utf-8 (from db for example) if my document has Content-Type: text/html; charset=ISO-8859-1 I mean htmlspecialchars()...
3
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I change the confirm box to say yes/no or default to cancel?...
3
by: vineet1987 | last post by:
i have already written my code using the neural network logic....and it works fine... it is basically a hand written character recognition system...which accepts "bitmap" image files which i made...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.