Jus S
06-30-2008, 11:54 AM
Hello all, I'm trying to create a script that has a dual purpose. The first is to create an input field that converts the value into upper case upon losing focus. The second shows the keys pressed using the following: event object for detecting the key, document.onkeypress to capture the event, and String.fromCharCode(Keycode) to convert the keycode obtained from the event back to a character. I accomplished the first goal but the second goal eludes me. Below is what I've been working with. What am I doing wrong?
Thanks,
Jus
<html>
<head>
<script type="text/javascript">
var x = function () {
document.getElementById('field').value = document.getElementById('field').value.toUpperCase();
}
otherCase = x;
otherCase();
</script>
<script type="text/javascript">
function DisplayKey (e) {
//which key has been pressed?
var keycode;
if (!e) var e = window.event;
else if (e.keyCode) keycode=e.keyCode;
else if (e.which) keycode=e.which;
character=String.fromCharCode(keycode)
// find the object for the destination paragraph
k = document.getElementById('keys')
document.onkeypress=DisplayKey(event);
// add the character to the paragraph
k.innerHTML += character;
}
</script>
</head>
<body>
<form>
<table align="center">
<tr><td><b>Input Field:</b></td>
<td><input name="fld" type="text" id="field" onblur="otherCase();"></td>
</tr>
</table>
</form>
<form type="text" id="keys" onkeypress="DisplayKey(event)"></form>
<h1>Display pressed keys</h1>
</body>
</html>
Thanks,
Jus
<html>
<head>
<script type="text/javascript">
var x = function () {
document.getElementById('field').value = document.getElementById('field').value.toUpperCase();
}
otherCase = x;
otherCase();
</script>
<script type="text/javascript">
function DisplayKey (e) {
//which key has been pressed?
var keycode;
if (!e) var e = window.event;
else if (e.keyCode) keycode=e.keyCode;
else if (e.which) keycode=e.which;
character=String.fromCharCode(keycode)
// find the object for the destination paragraph
k = document.getElementById('keys')
document.onkeypress=DisplayKey(event);
// add the character to the paragraph
k.innerHTML += character;
}
</script>
</head>
<body>
<form>
<table align="center">
<tr><td><b>Input Field:</b></td>
<td><input name="fld" type="text" id="field" onblur="otherCase();"></td>
</tr>
</table>
</form>
<form type="text" id="keys" onkeypress="DisplayKey(event)"></form>
<h1>Display pressed keys</h1>
</body>
</html>