I want to replace all the text in a webpage with saving the text length like:
From:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed id dui tincidunt
To:
sample text sample text sample text sample text sample text sample text sampl
I'am new in Javascript and I don't understand it. è_é
Please help me.
Unheilig
16.3k193 gold badges71 silver badges101 bronze badges
1 Answer 1
Create a function that takes the string of words to replace, and the string to repeat until the length, then simply keep appending to a variable until the length is reached:
function replaceTextWithLength(textToReplace, replaceText) {
var length = textToReplace.length;
var text = "";
while (text.length < length) {
text += replaceText;
}
return text.substring(0, length);
}
var words = replaceTextWithLength("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed id dui tincidunt", "sample text ");
console.log(words);
answered Apr 15, 2015 at 20:42
tymeJV
105k14 gold badges165 silver badges158 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Maarethyu
How can i get all the body text ? element.body.innerText ?
lang-js