1
document.getElementById("Message").innerHTML=document.getElementById("Message").innerHTML.replace("'","%%");

The above statement replaces only the first occurrence of the single quote. Could it be because that I do a submit right after that, and that javascript doesn't wait for the previous statement to be completed before moving on to the next one?

developer747
  • 15,419
  • 26
  • 93
  • 147

5 Answers5

3

Try using regex /g

.replace(/'/g,"%%")

Change your code as below,

document.getElementById("Message").innerHTML = 
                  document.getElementById("Message")
                          .innerHTML
                          .replace(/'/g,"%%");
Selvakumar Arumugam
  • 79,297
  • 15
  • 120
  • 134
0

To replace globally in javascript you need to add /g to the replacement string.

See this SO link How to replace all dots in a string using JavaScript

Community
  • 1
  • 1
iivel
  • 2,576
  • 22
  • 19
0

You should use regular expression in replace.

document.getElementById("Message").innerHTML=document.getElementById("Message").innerHTML.replace(/'/g,"%%");

jsfiddle

Anoop
  • 23,044
  • 10
  • 62
  • 76
0

Use the Global (g) flag in the replace() parameters.

See here

Alberto De Caro
  • 5,147
  • 9
  • 47
  • 73
0

You should use a regular expression and the /g (global) flag. This will replace all occurrences:

document.getElementById("Message").innerHTML=
    document.getElementById("Message").innerHTML.replace(/'/g,"%%");

http://www.w3schools.com/jsref/jsref_replace.asp

Pete
  • 2,538
  • 13
  • 15
  • [Handle W3Schools with care](http://stackoverflow.com/questions/8052158/alternative-to-w3schools-mozilla-developer-network) – Alberto De Caro Sep 26 '12 at 15:59
  • @ADC Yeah, but it offered a short, ambiguous answer accompanied with zero explanation and really great SEO. Though, I guess I'm just adding to the page rank with my reference. O_o – Pete Sep 26 '12 at 18:06