This should be pretty simple. I'm trying to use the slice method to remove the last two characters in a dynamically created string in a shopping cart.
So instead of having a product show as $28.00, I want the product to show up as $28. Since these values are coming from a database, I can't simply define the string in a variable, like I've seen in a lot of tutorials.
I've created a JSFiddle here: /
The jQuery that's not working is below:
$(".myclass").slice(0,-2);
This should be pretty simple. I'm trying to use the slice method to remove the last two characters in a dynamically created string in a shopping cart.
So instead of having a product show as $28.00, I want the product to show up as $28. Since these values are coming from a database, I can't simply define the string in a variable, like I've seen in a lot of tutorials.
I've created a JSFiddle here: http://jsfiddle.net/EbckS/
The jQuery that's not working is below:
$(".myclass").slice(0,-2);
Share
Improve this question
asked Mar 27, 2013 at 23:24
JaggsWaggertJaggsWaggert
1711 gold badge1 silver badge12 bronze badges
1
- 1 Don't you have to remove the last three to go from "$28.00" to "$28"? – Mike Samuel Commented Mar 27, 2013 at 23:29
2 Answers
Reset to default 18You should use text
.
$(".slice").text(function(i, text) {
return text.slice(0, -2);
});
i
Reffers the index position of the element in the settext
Reffers the old text value
Refference
Very Simple. I uses this for getting number from a string you can use it too.
var height= "800px";
var newheight = height.substring(0,height.length-2); //string 800
newheight= parseFloat(newheight) || 0; //number 800