...

View Full Version : remove textnode after it is created from the array



C.O.D.E.N.A.M.E
03-16-2006, 11:11 PM
I am asking if you could possibly help me out with this DOM/JS issue.

I have created an array that holds three textmessages, how can I
remove the created textNode and feed the next message in line? Is
there also an issue with cleaning any whitespace too?

A code snippet:
----------------------


var altTextBanners = new Array(
"myText1",
"myText2",
"myText3");

altTextBanners.currentIndex = -1;

function initRotate() {

if (!document.getElementById) return;

altTextBanners.currentIndex++;

var text = document.createTextNode(altTextBanners[altTextBanners.currentIndex]);
var message = document.getElementById("message");
message.appendChild(text);
// how to remove the created TextNode and get the next one in the array
// ... ??
}

HTML


<span id="message"></span>

Kravvitz
03-16-2006, 11:24 PM
You'll need to call that function with setTimeout() or setInterval() so that people will have time to read the text.

Try this.

message.removeChild(message.firstChild);

C.O.D.E.N.A.M.E
03-17-2006, 12:18 AM
I use setTimeout, simplified code here.
your idea only removes the created El

Kravvitz
03-17-2006, 01:08 AM
Yes, it only removes the text node.

I'm not sure why you need to ask how to add the next bit of text to the element. The other part is easy just call the function again.

If you want more help than that, please post your real code.

Kor
03-17-2006, 08:34 AM
but why create/append/remove when you can simply replace it using data method?
...
altTextBanners.currentIndex++;
document.getElementById("message").firstChild.data=altTextBanners[altTextBanners.currentIndex];
...

but make sure that initially the span has a first child already, let's say

<span id="message">&nbsp;</span>

Kravvitz
03-17-2006, 09:29 AM
Ah, you're right Kor. Using the data property of a text node is easier.

C.O.D.E.N.A.M.E
03-18-2006, 04:36 PM
How about this:




window.onload = initRotate;

var altTextBanners = new Array(
"myText1",
"myText2",
"myText3");

altTextBanners.currentIndex = 0;


function initRotate() {
if (!document.getElementById) return;

var text = document.createTextNode(altTextBanners[altTextBanners.currentIndex);
var message = document.getElementById("message");

if( message.firstChild ) {
message.replaceChild(text,message.firstChild);
} else {
message.appendChild(text);
}

setTimeout(initRotate, 1000);

altTextBanners.currentIndex++;
altTextBanners.currentIndex = altTextBanners.currentIndex %
altTextBanners.length;
}




this works fine

Kor
03-18-2006, 06:20 PM
if it works, it's OK. Still, if there is a shorter method, I should have chosen it.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum