Reputation: 1450
I need to remove first 3 characters from title. I used:
$(".layered_subtitle").each(function() {
var text = $(this).text();
text = text.replace('06.', '');
$(this).text(text);
I have dynamicly loaded titles with numbering:
01.title
02.title
03.title ..
How to to separate characters to be removed?
Upvotes: 1
Views: 5107
Reputation: 262
You could use substring method like this:
$(".layered_subtitle").each(function() {
var text = $(this).text();
if(text && text.length > 3){
text = text.substring(3)
$(this).text(text);
}
});
If you need to do this on ajax request success:
function removeChars(){
$(".layered_subtitle").each(function() {
var text = $(this).text();
if(text && text.length > 3){
text = text.substring(3)
$(this).text(text);
}
});
}
var serializedData = {};//Fill data
$.ajax({
url: "url",
type: "post",//post or get
data: serializedData,
// callback handler that will be called on success
success: function (){
//Add here logic where you update DOM
//(in your case add divs with layered_subtitle class)
//Now there is new data on page where we need to remove chars
removeChars();
}
});
Upvotes: 0
Reputation: 1765
If you don't know the exact position of separator (point) you must find it:
var newText = oldText.substr(oldText.indexOf('.') + 1);
Or, if you can find many digits or a space char after separator you need to use a regex:
var newText = oldText.replace(/^\d+\.\s*/, "");
Both would work.
Upvotes: 0
Reputation: 700262
You can remove the first three characters, if the title is at least three characters:
if (text.length >= 3) text = test.substring(3);
You can use a regular expression to only remove the numbers if it matches the format "nn.xxxx":
text = text.replace(/^\d\d\./, '');
Upvotes: 0
Reputation: 13
The answers so far are all right, but i think you should do it differently for future use. Maybe your count will extend, so just search for the first occurence of "." and then cut the string there.
text = text.substring(text.indexOf(".")+1);
Upvotes: 1
Reputation: 1450
Try this
text = text.substring(3)
instead of
text = text.replace('06.', '');
Upvotes: 0
Reputation: 28763
Try like this
text = text.substr(3);
alert(text);
$(this).text(text);
Upvotes: 0
Reputation: 14345
You can use this (shortest of the slice
/substring
/substr
-related JavaScript functions which are effectively the same if only using the first argument with a positive integer):
text = text.slice(3);
Upvotes: 1