0

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.

asked Apr 16, 2012 at 15:12
4
  • 1
    Just a note: you can use replace("£", "") Commented Apr 16, 2012 at 15:14
  • "It's not removing the pound sign." Not removing it where? It won't be shown in the alert, you're definitely removing it from the string you pass to alert (proof). That doesn't change anything else. Commented Apr 16, 2012 at 15:19
  • What are you trying to do, why are you removing the £ in the first place? Commented Apr 16, 2012 at 15:20
  • as I've said here: stackoverflow.com/a/10176894/643580, the code is working. you must have a markup or a script error elsewhere. Commented Apr 16, 2012 at 15:22

4 Answers 4

2

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'.

answered Apr 16, 2012 at 15:16
Sign up to request clarification or add additional context in comments.

5 Comments

I don't think the OP want's the replace the HTML (at least not the pound sign)... he just wants to extract the numeric value to perform calculations. edit: as it seems he want to put the changed value back, this could still be useful.
I think you mean var n = parseFloat(old); or simply var n = +old.replace('£', '');. Strings don't have a parseFloat method.
@FelixKling duh, yes... (blush)
I think I've found the error. If I view the pound sign in the source, it's not a pound sign, it's a "miss-matched character set" question mark. I can't replace the string I'm finding with £ then search for that, though. :( I get NaN when I run that code above.
@i-CONICA Try using '\xa3` instead of the £ symbol
2

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, ""));
answered Apr 16, 2012 at 15:15

3 Comments

I don't think the OP want's the replace the HTML... he just wants to extract the numeric value to perform calculations.
Correct Felix, I just want the numeric value so that I can perform a mathematical operation on it, then put it back modified.
@i-CONICA I see, Check the fiddle posted by stackoverflow.com/a/10176894/297641 seems to work. Check for any script error.
0

Nothing wrong. It is working here. Are you sure your markup is fine?

answered Apr 16, 2012 at 15:17

Comments

0

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.

answered Apr 16, 2012 at 15:19

1 Comment

Seems like that is not what OP wants.. see the comments in my answer.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.