0

I'm not having much luck in getting correct sorts with pricing as I belive the $ sign is interfering but I cannot get the replace function to work.

My HTML

<span class="field-content price">$486.15</span>

My jquery function

price : function( $elem ) {
        return parseFloat( $elem.find('.price').text().replace($, '') );
      }

For some reason this does not seem to be removing the $ sign. Am i missing something in the function?

Im also not sure if the decimal point could also be causing problems with the sort

Thanks in advance

Tushar Gupta - curioustushar
  • 54,013
  • 22
  • 95
  • 103

2 Answers2

0

DEMO

enclose $ in with in quotes '$'

price : function( $elem ) {
        return parseFloat( $elem.find('.price').text().replace('$', '') );
      }

DEMO

function x ($elem) {
    return parseFloat($elem.replace('$', ''));
}
$('.price').each(function(){
     $(this).text(x($(this).text()));
});
Tushar Gupta - curioustushar
  • 54,013
  • 22
  • 95
  • 103
  • Thanks for the reply, i tried the modification you suggested and it still does not work http://jsfiddle.net/NMvwb/1/ – Colin Lau Jul 31 '13 at 05:28
  • Your fiddle works great, however when i try put it into the rest of my function I'm getting errors, sorry if im missing something really simple here, thanks for your help so far, so close now! http://jsfiddle.net/A62LM/ – Colin Lau Jul 31 '13 at 06:17
0

Working example: http://jsfiddle.net/Vy9Cd/

You needed to enclose the $ in quotes:

      price: function x ($elem) {
          var price = parseFloat( $elem.find('.price').text().replace('$', '') );
          return price;
      }

However you also had a number of other issues:

  • HTML not matching the expected HTML by javascript
  • Javascript would not even compile
  • Missing isotope library

I recommend starting with a simple example and familiarizing yourself with handling syntax errors so that you can at least start with a working example.

Chris Herring
  • 3,575
  • 2
  • 30
  • 48