eagercoder
eagercoder

Reputation: 544

How do I replace one word for another in JS?

I am attempting to replace the word 'and' with 'dog', using JavaScript. Here is my code:

var magic = document.getElementById("magic");
function myFunction() {
    magic.innerHTML.replace('and', 'dog');
}
myFunction;

Upvotes: 0

Views: 2356

Answers (2)

klenium
klenium

Reputation: 2607

var magic = document.getElementById("magic");
function myFunction() {
    magic.innerHTML = magic.innerHTML.replace('and', 'dog');
}
myFunction();

Upvotes: 2

C3roe
C3roe

Reputation: 96250

replace does not change the input, it returns the changed value – so you have to assign it to magic.innerHTML again.

Upvotes: 4

Related Questions