0

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

2 Answers 2

2
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);
});
Sign up to request clarification or add additional context in comments.

3 Comments

okay what about for somestring.replace("/regex/","") ? regex to replace the last character.
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?
what is exactly the focus of your question?
1

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));.

1 Comment

doesn't work. still returns on the last character removed no matter how many times i click

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.