I have a somewhat strange situation where I need to fix a mistake on a website where, when creating a line (dynamically), it adds 5 spaces before the line and 5 spaces after the line. Obviously, the best thing would be to fix the end code and get rid of these spaces ... In short, I cannot, and I have to do it with javascript. I'm not quite sure how to do this, but this is what I was thinking about.
<span id="balance"> 245.34 </span>
My idea was this: javascript
function removespace()
{
var oldString = document.getElementById('balance');
var newString = (THIS IS WHERE I AM STUCK... I NEED TO REMOVE THE SPACES);
document.getElementByID('balance').innerHTML = newString;
}
Does anyone have any suggestions? Thank!
ALSO FORGET TO THE MENU: I cannot use javascript libraries such as prototype or jquery.
Edit: I still have this ... but it doesn't seem to work:
<span id="balance"> $245.00 </span>
<script>
function removespace()
{
var oldString = document.getElementById('balance');
var newString = oldString.trim ();
document.getElementByID('balance').innerHTML = newString;
}
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g,"");
}
</script>
here is the solution i used ... i finished it before i saw other updates ... but everyone was very helpful
function trim(stringToTrim) {
return stringToTrim.replace(/^\s+|\s+$/g,"");
}
var oldString = document.getElementById('balance').innerHTML;
var newString = trim(oldString);
document.getElementById('balance').innerHTML = newString;