Change all words in an HTML code

2

I need to do a Javascript function that changes all words found inside a website. I've tried something like this:

var elements = document.getElementsByTagName('div');
for (var i = 0; i < elements.length; i++) {
    var txt = elements.replace("Original","Alterada");
    document.getElementsByTagName(i).element.innerHTML = txt;
}

But unfortunately it did not work. Several errors appear. Any solution?

    
asked by anonymous 23.05.2014 / 16:54

1 answer

5

So:

var elements = document.getElementsByTagName('*');

for (var i = 0; i < elements.length; i++) {
    var txt = elements[i].innerHTML.replace("Original", "Alterada");
    elements[i].innerHTML = txt;
}

Note that this solution will only change the word "Original", not "original", nor "ORIGINAL", etc.

jsFiddle Example

As suggested by @Kazzkiq, the case-insensitive solution would look like this:

var elements = document.getElementsByTagName('*');

for (var i = 0; i < elements.length; i++) {
    var txt = elements[i].innerHTML.replace((new RegExp('(original)', 'gi')), "Alterada";
    elements[i].innerHTML = txt;
}

Example in jsFiddle

    
23.05.2014 / 17:04