Alex Rivera | Logout

Delete Last Char of String with Javascript

Asked 2010-01-02T01:23:28.463
18

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

Edit
Report

1 Answer

3

Edit: here's the easiest way to do this without any library dependencies

function removeLastChar(node) {
    var i = node.childNodes.length;
    while (--i >= 0) {
        if (3 === node.childNodes[i].nodeType) {
            node.childNodes[i].data = node.childNodes[i].data.replace(/\S\s*$/, '');
            break;
        }
    }
}

/\S\s*$/ still means "the last non-space at the end"

Note: borrowed from Tim Down's solution and further years of web development experience, :)

answered 2010-01-02T10:15:10.673

Your Answer