Reputation: 435
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
Upvotes: 0
Views: 3488
Reputation: 8638
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);
});
Upvotes: 2
Reputation: 4901
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));
.
Upvotes: 1