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