JavaScript .replace only replaces first Match
textTitle.replace(/ /g, '%20');
From w3schools
The replace() method searches for a match between a substring (or regular expression) and a string, and replaces the matched substring with a new substring
Would be better to use a regex here then:
textTitle.replace(/ /g, '%20');
You need a /g
on there, like this:
var textTitle = "this is a test";
var result = textTitle.replace(/ /g, '%20');
console.log(result);
You can play with it here, the default .replace()
behavior is to replace only the first match, the /g
modifier (global) tells it to replace all occurrences.