...

View Full Version : Javscript Opposite-Backspace Question



theflyingminstr
08-24-2007, 02: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)?

Thanks!

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


<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)" />
</form>

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

Thanks so much!!

Philip M
08-24-2007, 08: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!"
}
}
</script>


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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum