View Full Version : Javscript Opposite-Backspace Question

08-24-2007, 03:36 AM
Could someone point me in the right direction for making a key function that removes one character from the beginning of a string every time it is pressed (basically the exact opposite of a backspace key)?


08-24-2007, 04:56 AM
Here's an example:
<script type="text/javascript">
function revSpace(tfld, str) {
tfld.value = str.substr(1,str.length);

<form id="myform" method="post" action="" enctype="multipart/form-data">
<input type="text" id="txt" value="" size="50" maxlength="100" />
<input type="button" id=".revbut" value="Trim Front" onclick="revSpace(txt,txt.value)" />

08-24-2007, 08:12 AM
Oh, sweet - It works perfectly.

Thanks so much!!

Philip M
08-24-2007, 09:22 AM
The guy actually asked for a key, not a button. So here's the same thing but using the insert key to remove the first character. And for good measure pressing the delete key repeats the last character typed. Really useful, eh? :rolleyes:

<script type="text/javascript">

function revSpace(ev) {
ev = ev || event;
var key = ev.keyCode;
var str = document.myform.txt.value;
var len = str.length;
if (key == 45) { // insert key
document.myform.txt.value = str.substr(1,len);
if (key == 46) { // delete key)
document.myform.txt.value = str + str.substr(len-1, len)
if (key == 20) { // CapsLock key
document.myform.txt.value = "Idiot!"

<form name = "myform" method="post" action="" enctype="multipart/form-data">
<input type="text" name = "txt" value="" size="50" maxlength="100" onkeyup = "revSpace()">