I have a DIV with some characters. How can I remove the last character from the text with each click on the DIV itself?
9 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/
5 Comments
html from text, you will mis-handle < and & characters leading to potential XSS. Plus, fails when the last character in the text is \n, as . will not match a newline.Assuming you have a reference to the div and it just contains a single text node (as your question implies), this is very simple. Here's a non-jQuery solution:
div.onclick = function() {
var textNode = this.firstChild;
textNode.data = textNode.data.slice(0, -1);
};
6 Comments
this.firstChild.data is what you want to do (OP didn't say there were any children in the div, and does .data work in all browsers)? why not use this.innerHTML?<div>blah blah</div>, in which case the first (and only) child is a text node. The data property of text nodes is supported in all major browsers (including IE back to version 5). It's much quicker than using innerHTML because all it has to do is change one text property and re-render it in the document, while innerHTML has to build the innerHTML, parse it into a DOM node tree and then reinsert it into the document. data is also specified in the DOM standard and behaves uniformly across browsers, unlike innerHTML.data is DOM Level 1 Core and absolutely solid everywhere; it's fine as long as you're sure there's exactly one text node child. At least this answer manages not to allow HTML injection, and doesn't get stuck on newlines, unlike most of the others here!<div>I <i>loved</i> it!</div> as the last text node is no longer the one you want to mess with. Instead you have to grab the last childNode that has a nodeType of 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, :)
7 Comments
String.prototype.substr hexmen.com/blog/2009/12/javascript-date-to-time/… (link only works in FF)slice, not substr< or & character as it will delete the trailing ; of the &entref; instead of the whole character. A browser might then fix it up by putting the ; back.Alternative to Jonathan's answer, how to delete the first character:
$("div.myDiv").click(function(){
$(this).html($(this).text().substring(1));
});
Or, remove the last character:
$("div.myDiv").click(function(){
$(this).html($(this).text().replace(/.$/g, ''));
});
Or, go crazy and remove a character from a random place:
$("div.myDiv").click(function(){
var text = $(this).text();
var index = Math.round(Math.random() * (text.length - 1));
var result = text.substring(0, index) + text.substring(index + 1, text.length - 1);
$(this).html(result);
});
Instead of random place, you can use the above function with a predefined index to remove from a specific location.
4 Comments
This deletes a character each time you click on the div. Without more requirements, I can't give you any more.
<html>
<head>
<script>
function deleteChar(div) {
div.innerHTML = div.innerHTML.replace(/.$/, '');
}
</script>
</head>
<body>
<div onclick="deleteChar(this)">this is my div</div>
</body>
</html>
Oh, sorry, you asked for jquery... Well, this is how you do it in straight javascript.
Comments
If you wanted to delete the first character of the text every click you could use substring. e.g.:
$("#textDiv").click(function(){
$(this).html($(this).text().substring(1));
});
1 Comment
text, writes to html: if there are any < or & characters in there you've just changed their meaning and may have injected bad JavaScript into the page.Plain JavaScript:
<div id="text">This is some text</div>
<script>
var node = text.firstChild;
var nodeValue = "";
var nodeValueLength = 0;
text.onclick = function () {
nodeValue = node.nodeValue;
node.nodeValue = nodeValue.substring(0, nodeValue.length - 1);
};
</script>
3 Comments
text variable defined? Why build up the new node value a character at a time?text with document.getElementById("text"), since not all browsers (possibly only IE does) create references to elements from their IDs. Also, nodeValue.slice(0, -1) is more concise than the substring version.Its easy by using the attribute$= CSS3 selector which means "ends with" in your jQuery selection:
To Delete 1st character:
$("div[id$=/]").each(function(){
this.id = this.id.substr(1);
});
To Delete Last Character:
$("div[id$=/]").each(function(){
this.id = this.id.substr(0,this.id.length - 1);
});
3 Comments
/ have to do with this?
<div>text are you deleting the character from? From the end? from the start?