By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,605 Members | 2,153 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,605 IT Pros & Developers. It's quick & easy.

Cancel the character to be written?

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a

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 discussion thread is closed

Replies have been disabled for this discussion.