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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Feb 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question ...getElementById looping with XML...

    hello,

    i am fairly new to javascript and XML. i'm trying to loop through some nodes and drop the output into a series of DIVs using getElementById. here's the code for that:
    Code:
    <script language="JavaScript" type="text/JavaScript">
    function showCalendar(){
    if (document.implementation && document.implementation.createDocument)
    	{
    		xmlDoc = document.implementation.createDocument("","",null)
    	}
    	else if (window.ActiveXObject)
    	{
    		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
     	}
    	else
    	{
    		alert('Your browser can\'t handle this script');
    		return;
    	}
    xmlDoc.async=false;
    xmlDoc.load("calendar.xml");
    
    var days = xmlDoc.getElementsByTagName("day");
    
    for (var i=0; i<days.length; i++) {
    var calevent = days[i].getElementsByTagName("event")[0].childNodes[0].nodeValue;
    document.getElementById("event"+i).innerHTML = calevent;
    }
    }
    </script>
    and here is the XML (very basic):
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <calendar>
    <day>
    <date>1</date>
    <event>Event for the 1st</event>
    <link>http://www.eventlink.com</link>
    </day>
    <day>
    <date>2</date>
    <event>Event for the 2nd</event>
    <link>http://www.eventlink.com</link>
    </day>
    <day>
    <date>3</date>
    <event>Event for the 3rd</event>
    <link>http://www.eventlink.com</link>
    </day>
    </calendar>
    the part that is not working is document.getElementById("event"+i).innerHTML = calevent;. the error i'm getting is "...getElementById("event"+i)... has no properties", which tells me that the loop is not working correctly. but when i document.write(calevent) the loop works fine and outputs the XML. that doesn't suit my purposes. what i'm trying to do is loop through the nodes, grab the data and then flow it into a series of DIVs on the page (event1, event2, event3, etc.).

    the function loads onload from the body tag. the rest of the page is written in ASP.

    please advise.

    thanks for your time,
    dan.

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,074
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Quote Originally Posted by danh
    what i'm trying to do is loop through the nodes, grab the data and then flow it into a series of DIVs on the page (event1, event2, event3, etc.).
    If the divs have ids of event1, event2, event3 ..., then you should add 1 to i as i starts at 0.

    document.getElementById("event"+(i+1)).innerHTML = calevent;
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #3
    New to the CF scene
    Join Date
    Feb 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up ...that did the trick!...

    glenngv,

    thanks! i narrowed down the problem to the fact that the iteration was starting at 0 and the calendar starting at 1, but i couldn't see the answer!

    thanks again,
    dan.


  •  

    Posting Permissions

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