Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
174
rated 0 times [  178] [ 4]  / answers: 1 / hits: 36780  / 14 Years ago, wed, november 17, 2010, 12:00:00

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>

More From » regex

 Answers
13

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('&');



[#94929] Monday, November 15, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
francokeganr

Total Points: 438
Total Questions: 102
Total Answers: 124

Location: Belarus
Member since Sat, Jul 18, 2020
4 Years ago
;