...

View Full Version : Backspacing with JavaScript



Phalanxer
06-28-2012, 12: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 -= ''"

vwphillips
06-28-2012, 12:59 PM
<!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>

Phalanxer
06-28-2012, 01: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]


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


... then when toggling backspace:


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

Old Pedant
06-29-2012, 12: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