Forum Moderators: open

Message Too Old, No Replies

Very Simple Question

Javascript

         

jp_css

9:07 pm on Mar 31, 2004 (gmt 0)

10+ Year Member



I need to be able to type some text into one element of a form and have another element update itself based on the text. I tried various events but they execute "before" the text is in the box. Thus, it's always one letter behind. What's the best way to do this?

ajkimoto

9:20 pm on Mar 31, 2004 (gmt 0)

10+ Year Member



js_css,

So you want a function that will keep track of each keypress as it occurs? (Versus taking the final value of the textbox after the user is finished entering text).

ajkimoto

MozMan

9:24 pm on Mar 31, 2004 (gmt 0)

10+ Year Member



It's possible I'm not understanding the problem, but if I am, then I think you should be able to use the onBlur event to get the desired result.

For example, in your text box, you have onChange="JavaScript:UpdateTheOtherBox()"

Then, when you have finished typing in the text box and tab or click out of it, it calls a function that looks like this:

function UpdateTheOtherBox()
{
document.frmForm.textBox2.value = document.frmForm.textBox1.value
}

This may not be exactly right, but should give you the idea.

-Moz

jp_css

9:27 pm on Mar 31, 2004 (gmt 0)

10+ Year Member



That works fine, but I was looking for something to update it "on the fly" as the user is typing, if possible.

Rambo Tribble

9:30 pm on Mar 31, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not sure this is a simple question. Since the keyboard events capture information of the key pressed, not its associated character, it would seem you would have to write scripts to capture the events and map them to the appropriate character set.

Note you would need different scripts for the Mozilla and IE event models.

MozMan

9:33 pm on Mar 31, 2004 (gmt 0)

10+ Year Member



I see. Yeah, then onBlur won't work real well, as it won't call the function until you leave the text box. Perhaps onChange will work better. I may play with the idea and see what I can come up with. :)

MozMan

9:41 pm on Mar 31, 2004 (gmt 0)

10+ Year Member



Now I see what you have been experiencing when you said you were always one character behind.

I tried several event, and onKeyUp seems to be working. Try this out, and let me know what you think:

<html>
<head>

<script language="javascript">
function ChangeOtherBox()
{
document.frmForm.Box2.value = document.frmForm.Box1.value
}
</script>
</head>
<body>

<form name="frmForm">
<input type="text" name="Box1" onKeyUp="javaScript:ChangeOtherBox()">
<input type="text" name="Box2">
</form>

</body>
</html>

ajkimoto

9:47 pm on Mar 31, 2004 (gmt 0)

10+ Year Member



jp_css,

I agree with MozMan on the onKeyUp() event.

Here is some code to capture the actual key stroke (if you don't already have such...)

<script type="text/javascript">
<!--
function pressKey(e){
if(typeof event!='undefined'){
var pressedKey=window.event.keyCode
}else{
var pressedKey=e.keyCode
}
alert('The "'+(String.fromCharCode(pressedKey))+'" key was pressed')
}

//-->

</script>

</head>

<body>
<form>
<input type="text" size="30" value="" onkeyup="pressKey()" />
</form>
</body>

ajkimoto

jp_css

9:51 pm on Mar 31, 2004 (gmt 0)

10+ Year Member



onKeyUp() works perfectly. Thanks a lot.

Rambo Tribble

11:04 pm on Mar 31, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ajkimoto,

I think for your code to work, you have to use onkeypress. I believe it is the only keyboard event handler that returns character information.

ajkimoto

11:28 pm on Mar 31, 2004 (gmt 0)

10+ Year Member



Rambo Tribble,

You are partially correct. I neglected to add the appropriate event listener for it to work in Mozilla, though it works in IE as is. To add cross-browser compatibility, you would need to add the following to the script area:

document.addEventListener("keyup",pressKey,true);

This captures the keyup event and passes it on to the pressKey function.

ajkimoto

Rambo Tribble

1:23 am on Apr 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Right you are, akjimoto. You certainly have a handle on your events (all puns intended).