I've got a problem with replace not working.
alert($('input#membership_edc').next("label").html().replace(/£/g, ""));
The value of the label after the input would be 24ドル.00, but I want to do a math on that value then put it back with the new value. It's not removing the pound sign.
What's wrong? Thanks.
4 Answers 4
To read/modify/write use the function parameter version of .html():
$('input#membership_edc').next("label").html(function(index, old) {
var n = parseFloat(old.replace('£', '');
return n + 10;
});
would replace '24ドル.00' with '34'.
5 Comments
var n = parseFloat(old); or simply var n = +old.replace('£', '');. Strings don't have a parseFloat method.You need to set the value returned from the replace. Try below,
var $label = $('input#membership_edc').next("label");
$label.html($label.html().replace(/£/g, ""));
3 Comments
Nothing wrong. It is working here. Are you sure your markup is fine?
Comments
The string replace function returns a new string. Strings themselves are immutable in javascript, I think. As Vega has it:
// avoid looking this up twice
var label = alert($('input#membership_edc').next("label");
// replace and assign
label.html(label.html().replace(/£/g, ""));
Edit:
To get the numerical value from the string:
var amount = alert($('input#membership_edc').next("label").html().match(/£\s*([-0-9.]+)/)[1];
This matches the numbers etc after the £ (ignoring whitespace), and uses index 1 from the array, containing the contents of the first group match (in the brackets).
" 1ドル.45".match(/£\s*([-0-9.]+)/)[1]; // returns "1.45"
Beware that it is still a string, so you might want to do parseFloat on it.
alert, you're definitely removing it from the string you pass toalert(proof). That doesn't change anything else.£in the first place?