View Full Version : Backspacing with JavaScript

06-28-2012, 01:04 PM
Does anyone know a simple way to toggle backspace on a HTML INPUT value?

I tried this, but it didn't work:

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

06-28-2012, 01:59 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<script type="text/javascript">

function BackSpace(id){
var obj=document.getElementById(id);


<input id="tom" value="123456789" /> <input type="button" name="" value="Back" onmouseup="BackSpace('tom')" />


06-28-2012, 02:14 PM
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]

var $foobarprior='document.forms['foo']['bar'].value';
document.forms['foo']['bar'].value += '1';

... then when toggling backspace:

document.forms['foo']['bar'].value = $foobarprior;

Old Pedant
06-29-2012, 01:34 AM
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.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum