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
  • 14,200
  • 23
  • 84
  • 142

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
  • 78,145
  • 14
  • 119
  • 133
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,566
  • 22
  • 19
0

You should use regular expression in replace.

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

jsfiddle

Anoop
  • 22,496
  • 10
  • 60
  • 70
0

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

See here

Alberto De Caro
  • 5,049
  • 9
  • 43
  • 72
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,488
  • 10
  • 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