0

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?

1 Answer 1

1

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");
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.