tags:

views:

72

answers:

2

in my bind "click" event, i would like to chop the last character off a given string incrementally. however, each time i click it, it seems to stop after the first chomp.

var somestring = "mary had a little lamb";
$("#removelaststring").unbind("click").bind("click",function(e){

alert(somestring.slice(0,-1))

});

it should return "one less character" each time it's clicked

+1  A: 

somestring.slice(0,-1) gives you a new string without the last letter, but it doesn't modify somestring. Try alert(somestring = somestring.slice(0,-1));.

No Surprises
doesn't work. still returns on the last character removed no matter how many times i click
wghwh
+2  A: 
var somestring = "mary had a little lamb";

// you can use .click( function ) instead of .bind("click", function)
$("#removelaststring").click(function(e){
  // slice & save it
  somestring = somestring.slice(0,-1);
  alert(somestring);
});
Anwar Chandra
okay what about for somestring.replace("/regex/","") ? regex to replace the last character.
wghwh
when you say replace the last character using regex, do you want to build the regex at runtime with a pattern that you can specify? Also, do you literally mean the last character or characters matched by the regular expression pattern?
Russ Cam
what is exactly the focus of your question?
Anwar Chandra