Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Feb 2006
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    remove textnode after it is created from the array

    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:
    ----------------------

    PHP Code:
    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
    PHP Code:
    <span id="message"></span

  • #2
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You'll need to call that function with setTimeout() or setInterval() so that people will have time to read the text.

    Try this.
    Code:
    message.removeChild(message.firstChild);
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

  • #3
    New Coder
    Join Date
    Feb 2006
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I use setTimeout, simplified code here.
    your idea only removes the created El
    Last edited by C.O.D.E.N.A.M.E; 03-17-2006 at 02:02 AM.

  • #4
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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>
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #6
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ah, you're right Kor. Using the data property of a text node is easier.
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

  • #7
    New Coder
    Join Date
    Feb 2006
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts
    How about this:

    PHP Code:

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

    altTextBanners.currentIndex++;
    altTextBanners.currentIndex altTextBanners.currentIndex %
    altTextBanners.length;

    this works fine
    Last edited by C.O.D.E.N.A.M.E; 03-18-2006 at 05:43 PM.

  • #8
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    if it works, it's OK. Still, if there is a shorter method, I should have chosen it.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •