1

I am trying to manipulate the text that is already inside the div and replace it with another strings from JavaScript, creating a basic animating effect by replacing particular character from strings present inside the div element, more precisely position based replacement of string. However when I wrote down the following code it doesn't work. What I wanted to do is that store the original text in one variable and when the dom replacement of text occurs then setting certain interval replace by the old text and again replace by the new randomize text creating text replacement animation in certain position of the text.

code :

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var all ="ABCDEFGHIJKLMNOPQRSTUVWXYZa{}[];:></?bcdefghijklmnopqrstuvwxyz0123+_)(*&^%$#@!~`456789";
var old_text = document.getElementById("text").innerText;

function randChar() {
    "use strict";
    return all.charAt(Math.floor(Math.random()*all.length));   
}
function main() {
    "use strict";
    var $_inter = setInterval(function() {

        var text = document.getElementById("text");

        text.innerHTML = text.innerHTML.substring(0, 5) + randChar() + text.innerHTML.substring(5);
        setTimeout(function(){
            text.innerHTML = old_text;
        },200);
    }, 350);
}
window.onload = main;
</script>
</head>
<body>
<div id="text">Hello World!</div>
</body>
</html>

So in order to make it work for a while I used the original string as

setTimeout(function(){
            text.innerHTML = "Hello World!";
        },200);

Which is not possible as the text in page might have been generated dynamically. When I did run the first code it says innerText of Null.

The exact error it throws is:

Uncaught TypeError: Cannot read property 'innerText' of null

What does that mean, because text and element is there why can't it grab the text from dom?

7
  • text is not set inside of the setTimeout()-function? Commented Mar 31, 2013 at 6:56
  • shouldn't it be able to grab from the top declared old_text as every other variable can be accessed like the 'all' can be accessed from randChar() why old_text can't be ? Commented Mar 31, 2013 at 6:58
  • What is the use of the flag variable? Commented Mar 31, 2013 at 7:05
  • @sann sorry about that, i forgot to remove it, it not functional in this code, i will update it right now Commented Mar 31, 2013 at 7:06
  • @monk there ain't no use of counter variable too. Commented Mar 31, 2013 at 7:07

2 Answers 2

5

Cannot read property 'innerText' of null

Your problem is being cause because you're getting #text before it is defined.

var old_text = document.getElementById("text").innerText;

You should include this in your window.onload function as it will exist then.

Uncaught ReferenceError: flag is not defined

Once you do that you will receiving another error:

Uncaught ReferenceError: flag is not defined

For the line:

flag = flag+1;

This is because you have not defined your flag variable, this can be fixed by defining var flag; at the top of your first.

Demo

jsFiddle

var all ="ABCDEFGHIJKLMNOPQRSTUVWXYZa{}[];:></?bcdefghijklmnopqrstuvwxyz0123+_)(*&^%$#@!~`456789";
var old_text;
var flag = 0;

function randChar() {
    "use strict";
    return all.charAt(Math.floor(Math.random()*all.length));   
}
function main() {
    "use strict";
    old_text = document.getElementById("text").innerText;
    var counter = Math.floor(Math.random()*document.getElementById("text").innerText.length);
    var $_inter = setInterval(function() {

        var text = document.getElementById("text");

        text.innerHTML = text.innerHTML.substring(0, 5) + randChar() + text.innerHTML.substring(5);
        setTimeout(function(){
            text.innerHTML = old_text;
        },200);
        flag = flag+1;
    }, 350);
}
window.onload = main;

Error in Firefox

With the above code we're still receiving the original error in Firefox. This is because Firefox doesn't implement innerText. This can be fixed by using either .textContent or .innerHTML.

jsFiddle

old_text = document.getElementById("text").textContent;
var counter = Math.floor(Math.random()*document.getElementById("text").textContent.length);
// or
old_text = document.getElementById("text").innerHTML;
var counter = Math.floor(Math.random()*document.getElementById("text").innerHTML.length);
Sign up to request clarification or add additional context in comments.

3 Comments

this throws a TypeError: document.getElementById(...).innerText is undefined error on var counter = Math.floor(Math.random() * document.getElementById("text").innerText.length); in firefox 19.0.2 for me. If I replace innerText with innerHTML in said line, the output is "undef_ned" with "_" being replaced with random chars. Any thoughts?
yes, i couldn't run it on firefox as well, but in chrome it works perfectly
Perfect. +1 for explanation.
1
var all ="ABCDEFGHIJKLMNOPQRSTUVWXYZa{}[];:></?bcdefghijklmnopqrstuvwxyz0123+_)(*&^%$#@!~`456789";
var old_text;

function randChar() {
    "use strict";
    return all.charAt(Math.floor(Math.random()*all.length));   
}
function main() {
    "use strict";
    old_text = document.getElementById("text").innerText;
    var $_inter = setInterval(function() {
        var text = document.getElementById("text");
        text.innerHTML = text.innerHTML.substring(0, 5) + randChar() + text.innerHTML.substring(5);
        setTimeout(function(){
            text.innerHTML = old_text;
        },200);
    }, 350);
}
window.onload = main;

Just get rid of flag and counter. Initialize old_text in the onload listener.

1 Comment

In firefox 19.0.2, the output is undef_ned with _ being replaced with random chars.

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.