Reputation: 15673
I have a simple jquery function as
function sendValue(str){
$.post("test.php",{ sendValue: str },
function(data){
$('#display').html(data.returnValue);
}, "json");
}
I want to replace space within the string. I thought I can do this by adding
str.replace(" ", "+");
to the second line of the function. But it did not work. I know very basic of javascript. How to replace " " with "+" in the string before posting data to test.php?
Upvotes: 0
Views: 199
Reputation: 25445
Try str.replace(/\s/g , "+")
instead. (/\s/
is the regex escape for whitespace).
Also are you trying to encode the string as a URL? You could use encodeURIComponent(str)
which is a built in javascript method.
Upvotes: 2
Reputation: 35194
I think it should be the opposite
str.replace("+", " ");
I would try using
.replace(/ /g," ");
though.
Upvotes: -1