0

This is my code it replaces only one word at a time. I want it to replace all the words at once.

<!DOCTYPE html>
<html>
<body>

<p>Replace "Microsoft" with "google" in the paragraph below:</p>

<button onclick="myFunction()">Try it</button>

<p id="demo">Please visit  Microsoft Microsoft Microsoft Microsoft!</p>

<script>
function myFunction() {
    var str = document.getElementById("demo").innerHTML; 
    var txt = str.replace("Microsoft","google");
    document.getElementById("demo").innerHTML = txt;
}
</script>

</body>`enter code here`
</html>
Raghu
  • 817
  • 1
  • 7
  • 21

1 Answers1

0

try this

 function myFunction() {
 var str = document.getElementById("demo").innerHTML;
 var temptext = str.split("Microsoft").join("google");
 document.getElementById("demo").innerHTML = temptext ;
}
rjdmello
  • 865
  • 2
  • 9
  • 14