Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Removing last two characters in a class

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);
like image 227
JaggsWaggert Avatar asked Mar 27 '13 23:03

JaggsWaggert


People also ask

What is slice () method in jQuery?

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.

How do I remove the last character of a string?

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.


1 Answers

You should use text.

$(".slice").text(function(i, text) {
    return text.slice(0, -2);
});
  • i Reffers the index position of the element in the set
  • text Reffers the old text value

Refference

like image 132
Ricardo Alvaro Lohmann Avatar answered Oct 19 '22 22:10

Ricardo Alvaro Lohmann