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);
slice() method constructs a new jQuery object containing a subset of the elements specified by the start and, optionally, end argument. The supplied start index identifies the position of one of the elements in the set; if end is omitted, all elements after this one will be included in the result.
The easiest way is to use the built-in substring() method of the String class. In order to remove the last character of a given String, we have to use two parameters: 0 as the starting index, and the index of the penultimate character.
You 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 valueRefference
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With