views:

33

answers:

2

Hi,

I have the following problem:

I try to animate text so that it shows up letter by letter. This works for me like this:

var a = 0, speed = 85, text = [], story = "bla bla... text";
function tickertext(){
obj = document.getElementById("textbox");
text[a] = document.createTextNode(story.charAt(a));
obj.appendChild(text[a]);
a++;
a != story.length && setTimeout(function(){tickertext()}, speed)}
if(window.addEventListener) {
window.addEventListener("click", tickertext, false)}
else{window.attachEvent && window.attachEvent("onclick", tickertext)};

the html part looks like this:

<button onClick="tickertext()">Next</button>

  <table>
    <tr>
      <td>
        <div id="image">
          <img src="img/background1.jpg" width="600" height="400"><br>
          <br>
          <br>
          <p id="textbox" style="padding-left:25px; width:550px;"><br></p>
          <canvas id="mycanvas" width="600" height="400"></canvas>
        </div>
      </td>
    </tr>
  </table>

What I get is some text (in this case coming from a variable but in the end it is supposed to come from a xml file) that is displayed bit by bit. The goal is to start displaying the first part of the text after clicking on the button and then remove that whole text again after clicking on the button again and replacing it with other text that gets animated in the same way as the first part. That should be possible over and over again with different pieces of text (it's planned to get that text all from one xml file one part after the other).

The script you see here was pre-made and I am actually not that good with javascript. I tried some stuff with jquery and just normal javascript to remove the text again but it just didn't work out for me... but then again my knowledge about javascript is limited.

I would be happy about any solution.

A: 

Try removing all the nodes from the textbox element then running your script again with different text:

([edit] here's a full html file which should work; try starting from there)

<html>
<head>
<title>Test</title>
<script type="text/javascript">
var a = 0, speed = 85, text = [], story = "bla bla... text";
function tickertext(){
  obj = document.getElementById("textbox");
  text[a] = document.createTextNode(story.charAt(a));
  obj.appendChild(text[a]);
  a++;
  a != story.length && setTimeout(function(){tickertext()}, speed)
}
/*if(window.addEventListener) {
window.addEventListener("click", tickertext, false)}
else{window.attachEvent && window.attachEvent("onclick", tickertext)};*/
function clrtext(){
var tbox = document.getElementById("textbox");

if ( tbox.hasChildNodes() )
{
    while ( tbox.childNodes.length >= 1 )
    {
        tbox.removeChild( tbox.firstChild );       
    } 
}
a=0;
}
</script>
</head>
<body>
<button onClick="tickertext()">Next</button>
<button onClick="clrtext()">Clear</button>

  <table>
    <tr>
      <td>
        <div id="image">
          <img src="img/background1.jpg" width="600" height="400"><br>
          <br>
          <br>
          <p id="textbox" style="padding-left:25px; width:550px;"><br></p>
          <canvas id="mycanvas" width="600" height="400"></canvas>
        </div>
      </td>
    </tr>
  </table>
</body>
</html>

(source for clearing child nodes)

NickAldwin
Thanks for your answer but unfortunately adding this piece of code to my javascript file removes not only the text (I'm not even able to start the text-animation anymore) but also the canvas I put under the text.
hdr
@hdr I don't see why it would remove the canvas...it's not a child of textbox in the HTML you show above.
NickAldwin
I'm not too sure myself. Maybe it would help if I tell you what extra plugins I'm using... maybe there is a conflict.I am using "scriptaculous" with another button to hide the text and the canvas to make only the background visible then there is also jquery and prototype (needed for scriptaculous).The code for the other buttons:<button onClick="Effect.Fade('textbox');Effect.Fade('mycanvas')">Hide</button><button onClick="Effect.Appear('textbox');Effect.Appear('mycanvas')">Show</button>But I can't imagine that this would influence the animation... or does it?
hdr
ps: sorry of course I mean the canvas (and it's disappearing) and not "the animation"
hdr
I tried running this, and to get it to work properly I had to temporarily comment out the part where you make it trigger the ticker upon window click. But see my edit. You might be able to start from there.
NickAldwin
A: 

Great, many thanks! Unfortunately I cannot mark this as answered because I asked this while being unregistered and the whole thing timed out over night but it works and I'm happy about it. :)

hdr