Removing dollar signs from prices

I'm building a string of amounts but need to remove the dollar signs. I have this jQuery code:

  buildList($('.productPriceID > .productitemcell'), 'pricelist')

It's returning

pricelist=$15.00,$19.50,$29.50

I need to remove the dollar signs but can't seem to figure it out. Tried using .trim but I think that removes only white space.

Sorry for the newbie question! Thanks in advance for any help!

Here's the full code:

function buildList(items, name) {
var values = [];
items.each(function() {
values.push(this.value || $(this).text());
});
return name + '=' + values.join(',');
}

var result = [
buildList($('.productCodeID > .productitemcell'), 'skulist'),
buildList($('.productQuantityID > .productitemcell > input'), 'quantitylist'),
buildList($('.productPriceID > .productitemcell'), 'pricelist')
];

var string = result.join('&');

Here is the raw code before the javascript runs

<span class="productPriceID">
<div class="productitemcell">$15.00</div>
<div class="productitemcell">$19.50</div>
<div class="productitemcell">$29.50</div>
</span>

Answers:

Answer

EDIT: Starting over with answer now that I have the code that is running.

Looking at your updated code, this should work:

Example: http://jsbin.com/ekege3/

var result = [
    buildList($('.productCodeID > .productitemcell'), 'skulist'),
    buildList($('.productQuantityID > .productitemcell > input'), 'quantitylist'),
    buildList($('.productPriceID > .productitemcell'), 'pricelist')
];

result[ 2 ] = result[ 2 ].replace(/\$/g, '');

var string = result.join('&');

Side note: You can shorten your buildList function a little like this:

function buildList(items, name) {
    return (name + '=') + items.map(function() {
        return (this.value || $(this).text());
    }).get().join(',');
}

Original answer:

If you have a string, just use .replace().

var str = "pricelist=$15.00,$19.50,$29.50";

str = str.replace(/\$/g, '');

Or are you saying that you have a variable pricelist containing an Array? If so, do this:

var pricelist = ["$15.00","$19.50","$29.50"];

for( var i = 0, len = pricelist.length; i < len; i++ ) {
    pricelist[ i ] = pricelist[ i ].replace('$', '');
}

EDIT: It sounds as though the buildList method returns an Array.

One way to check would be to do this:

alert( Object.prototype.toString.call( result[2] ) );

And see what it gives you.

Anyway, assuming it's an Array, here's the updated version of the second example.

var result = [
    buildList($('.productCodeID > .productitemcell'), 'skulist'),
    buildList($('.productQuantityID > .productitemcell > input'), 'quantitylist'),
    buildList($('.productPriceID > .productitemcell'), 'pricelist')
];

// verify the data type
alert( Object.prototype.toString.call( result[ 2 ] ) );

// loop over result[ 2 ], replacing the $ with ''
for( var i = 0, len = result[ 2 ].length; i < len; i++ ) {
    result[ 2 ][ i ] = result[ 2 ][ i ].replace('$', '');
}

var string = result.join('&');

Answer
var price = $("div").text().replace("$", "");

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.