Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete Last Char of String with Javascript

I have a DIV with some characters. How can I remove the last character from the text with each click on the DIV itself?

like image 929
Mikkel Avatar asked Jan 02 '10 01:01

Mikkel


1 Answers

Removing First Character

​$("div").on("click", function(){
    $(this).text(function(index, text){
        return text.replace(/^.(\s+)?/, '');
    });
});​​​​​​​​​​​​

Removing Last Character

$("div").on("click", function(){
    $(this).text(function(index, text){
        return text.replace(/(\s+)?.$/, '');
    });
});

Removing a Specific Char

$("div").on("click", function(){
    $(this).text(function(index, text){
        return text.replace(/r/gi, '');
    });
});

See an example of each online at: http://jsfiddle.net/Xcn6s/

like image 81
Sampson Avatar answered Oct 09 '22 05:10

Sampson