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
    Banned
    Join Date
    Jun 2012
    Posts
    81
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Backspacing with JavaScript

    Does anyone know a simple way to toggle backspace on a HTML INPUT value?

    I tried this, but it didn't work:

    Code:
    onclick="document.forms['foo']['bar'].value -= ''"

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,466
    Thanks
    3
    Thanked 495 Times in 482 Posts
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <script type="text/javascript">
    /*<![CDATA[*/
    
    function BackSpace(id){
     var obj=document.getElementById(id);
     obj.value=obj.value.substring(0,obj.value.length-1);
    }
    /*]]>*/
    </script></head>
    
    <body>
    
    <input id="tom" value="123456789" /> <input type="button" name="" value="Back" onmouseup="BackSpace('tom')" />
    </body>
    
    </html>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • Users who have thanked vwphillips for this post:

    Phalanxer (06-28-2012)

  • #3
    Banned
    Join Date
    Jun 2012
    Posts
    81
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Thanks for that code man, but its way hard for me to digest.


    I tried this, but it didn't work! I can't believe it!!:

    Ttoggling a button that will insert '1'):[/U]
    Code:
    onclick="
    	var $foobarprior='document.forms['foo']['bar'].value';
    	document.forms['foo']['bar'].value += '1';
    "
    ... then when toggling backspace:
    Code:
    onclick="
    	document.forms['foo']['bar'].value = $foobarprior;
    "
    Last edited by Phalanxer; 06-28-2012 at 01:37 PM.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    It shouldn't be hard to understand.

    You get the current value of the field. You find out how long it is. Say it is 13 characters long. So then you use substring to get just the first 12 characters and store only those 12 back in the field.

    If the ONLY way that values can end up in that field is by way of the buttons you control, then your code will work. But if, for example, a person could use copy/paste to put stuff in the field, you code won't work.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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