This one seems simple, but I have no idea why it's not working. I would like to replace part of a string in a variable.
Say, I have this code:
var url = "At vero eos et accusam et justo duo dolores et ea rebum.";
url.replace('At vero eos et accusam et ', '');
Why doesn't it work? This one works:
var url = "At vero eos et accusam et justo duo dolores et ea rebum.".replace('At vero eos et accusam et ', ''):
-- but I'm using the variable elsewhere, so I would like to keep the replace out of the variable definition.
Fiddle here.
You need to save the replaced string
var url = "At vero eos et accusam et justo duo dolores et ea rebum.";
url = url.replace('At vero eos et accusam et ', '');
//^^^^^this
DEMO
Updated FIDDLE
var url = "At vero eos et accusam et justo duo dolores et ea rebum.";
url=url.replace('At vero eos et accusam et ', '');
$('#box').text(url);
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