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>
To get rid of the dollar sign, first select the cells with dollar signs you want to remove. Then, in the Toolbar, click on Format > Number > Number. As a result, the dollar sign is deleted from the selected cells.
In the Data Type Format dialog box, do one of the following: To add a currency symbol, click Currency, and then in the Currency list, select the type of currency that you want to display. To remove a currency symbol, click Number.
Dollar signs can also be removed from a dataframe column or row, by using the gsub() method. All the instances of the $ sign are removed from the entries contained within the data frame.
replace('$','') method on the entire column. This is the most straightforward method, as it simply replaces the '$' with a blank space for each item in the column.
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('&');
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