I have a DIV
with some characters. How can I remove the last character from the text with each click on the DIV
itself?
views:
2003answers:
9Removing First Character
$("p").click(function(){
// removes the first character (and orphaned spaces) on each click
$(this).html( $(this).text().replace(/^.(\s+)?/, "") );
});
Removing Last Character
$("p").click(function(){
// removes the last character (and orphaned spaces) on each click
$(this).html( $(this).text().replace(/(\s+)?.$/, "") );
});
Removing a Specific Char
$("div.myDiv").click(function(){
// replace all r's (both big and small) with o's.
$(this).html( $(this).text().replace(/r/gi, "o") );
});
Of course you'd have to be careful not to forget <br/>
and <hr/>
and <strong>
, etc, which all include that single character, and also may exist within your div.
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.
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.
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));
});
To supplemnt on Jonathons answer, use..
$("div.myDiv").click(function(){
$(this).html( $(this).text().replace("/.$/gi", "o") );
});
To remove the last character
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);
});
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>
I think everyone's making this slightly too complicated. If there is no whitespace (and you should be able to control this...):
$( '#thediv' ).click( function( ){ this.innerHTML = this.innerHTML.slice(0,-1); } );
if there is whitespace:
$( '#thediv' ).click( function( ){ this.innerHTML = this.innerHTML.replace(/\S\s*$/,''); } );
/\S\s*$/
means "the last non-space at the end"
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);
};