Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jul 2007
    Posts
    191
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Javscript Opposite-Backspace Question

    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!

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Posts
    679
    Thanks
    0
    Thanked 16 Times in 15 Posts
    Here's an example:
    Code:
    <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>

  • #3
    Regular Coder
    Join Date
    Jul 2007
    Posts
    191
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Sweet

    Oh, sweet - It works perfectly.

    Thanks so much!!

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,309
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    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?


    <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>
    Last edited by Philip M; 08-24-2007 at 09:46 AM. Reason: Added another gimmick


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •