user1592380
user1592380

Reputation: 36317

regex string replace with javascript in document

I'm using JS and want to do a string replace of an entire HTML page. I've tried:

var swapIn = 'value="teststring"';
var myOldString = (document.querySelectorAll('html')[0].outerHTML);
var myNewString = myOldString.replace(/value="[^"]*"/g, swapIn);
document = myNewString;

The switch is not occurring, How can I fix this?

Upvotes: 0

Views: 74

Answers (1)

gp.
gp.

Reputation: 8225

you cannot replace document. rather try modifying body innerHTML.

var swapIn = 'value="teststring"';
var myOldString = document.body.innerHTML;
var myNewString = myOldString.replace(/value="[^"]*"/g, swapIn);
document.body.innerHTML = myNewString;

if you are replacing value, I guess you want to change input elements isn't it? A better approach I would suggest would be:

$("input").each(function(){
   $(this).val("teststring");
});

Upvotes: 1

Related Questions