JQuery Removing the last two characters in a class

It should be pretty simple. I am trying to use the slice method to delete the last two characters in a dynamically created line in a shopping cart.

So, instead of showing the product as $ 28.00, I want the product to show as $ 28. Since these values ​​come from the database, I cannot just define a string in a variable, as I saw in many tutorials.

I created a JSFiddle here: http://jsfiddle.net/EbckS/

JQuery, which does not work, is below:

$(".myclass").slice(0,-2); 
+6
source share
2 answers

You must use text .

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

Refference

+17
source

Very simple. I use this to get a number from a string, which you can also use.

 var height= "800px"; var newheight = height.substring(0,height.length-2); //string 800 newheight= parseFloat(newheight) || 0; //number 800 
0
source

All Articles