wghwh
wghwh

Reputation: 435

jquery: bind "click" event

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

Answers (2)

Anwar Chandra
Anwar Chandra

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

No Surprises
No Surprises

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

Related Questions