How do I turn all the text in a page into a single string of characters, keeping all the CSS?
Suppose I have a page like this:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>My Document</title>
</head>
<body>
<h1>This is some text</h1>
<h2>This is some smaller text</h2>
<h3>This is even smaller text</h3>
</body>
<script>
// put the code here
</script>
</html>
I want to turn all the text into a single string, for example, the letter "A".
How do I do that?
I tried running document.body.innerText = "A"
, but the entire page only turned into a single "A".
I was expecting the whole page’s text to be filled with A’s, but only one "A" actually rendered.
3
Answers
This code selects all elements in the body of the HTML document using
document.body.getElementsByTagName("*")
. Then, it loops through each element and replaces its inner text with the letter "A" repeated for the length of the original inner text. This is likely a test or demonstration of manipulating the DOM using JavaScript. Hope it helps you!Loop through all the elements on the page. If the element only contains a single text node, then it’s a leaf element with text. Replace its text with
A
.you can use JS and implement something like like