0

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
asked Apr 15, 2015 at 20:34

1 Answer 1

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);

Demo: http://jsfiddle.net/zdvz59yb/

answered Apr 15, 2015 at 20:42
Sign up to request clarification or add additional context in comments.

1 Comment

How can i get all the body text ? element.body.innerText ?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.