JavaScript - Childnode Help?
Hi, I almost have my code working however it's not working because the div's have the same id, because they are created automatically through php so I cannot name them myself. Here is my code:
Code: <style> div{border: 1px solid black; padding: 5px;} </style> <script> function show(navigation) { var alli = document.getElementById('navigation').getElementsByTagName('a'); aam.value = alli[0] mel.value = alli[1] ipc.value = alli[2] } </script> <div id="navigation" onClick="show()"> <a href="http://www.google.com">Google.com</a> <br> <a href="http://www.yahoo.com">Yahoo.com</a> <br> <a href="http://www.bing.com">Bing.com</a> </div> <br> <div id="navigation" onClick="show()"> <a href="http://www.google.com">jack.com</a> <br> <a href="http://www.yahoo.com">ning.com</a> <br> <a href="http://www.bing.com">bill.com</a> </div> <br> <input type=text id=aam> <input type=text id=mel> <input type=text id=ipc> I'm trying to make it so that when I click one a div, its specific contents get transferred to the input boxes below, it's doing so, however it only works for the first div. How would I make it work for all of the divs to correspond with its own specific children(a tags)? Similar TutorialsHi I'm working on displaying a list of events on my site and need to display them in ascending order. i've got an xml document that looks like this(with multiple events obviously, i've just put one here): Code: <EventList> <Event> <Title>title example</Title> <myDescription>description example</myDescription> <myLink>http://www.example.com</myLink> <dayDate>10</dayDate> <monthDate>5</monthDate> <yearDate>2010</yearDate> <EventPic>Event.gif</EventPic> </Event> </EventList> Then on my html page, I have this javascript: Code: <script type="text/javascript"> if (window.XMLHttpRequest) { xhttp=new XMLHttpRequest(); } else // Internet Explorer { xhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xhttp.open("GET","Events.xml",false); xhttp.send(""); xmlDoc=xhttp.responseXML; var jDate=new Date(); var jYear=jDate.getFullYear(); var jYearStr=jYear.toString(); var jMonth=jDate.getMonth()+1; var jMonthStr=jMonth.toString(); var jDay=jDate.getDate(); var jDayStr=jDay.toString(); var currentDate=jYearStr+jMonthStr+jDayStr; document.write("<div>"); var x=xmlDoc.getElementsByTagName("Event"); for (i=0;i<x.length;i++) { var theMonthDate=x[i].getElementsByTagName("monthDate")[0].childNodes[0].nodeValue; var theDayDate=x[i].getElementsByTagName("dayDate")[0].childNodes[0].nodeValue; var theYearDate=x[i].getElementsByTagName("yearDate")[0].childNodes[0].nodeValue; var theDate=theYearDate+theMonthDate+theDayDate; if (theDate>=currentDate) { document.write("<br>"); document.write("<a href='"); document.write(x[i].getElementsByTagName("myLink")[0].childNodes[0].nodeValue); document.write("'>"); document.write(x[i].getElementsByTagName("Title")[0].childNodes[0].nodeValue); document.write("</a>"); document.write("<br>"); document.write(x[i].getElementsByTagName("myDescription")[0].childNodes[0].nodeValue); document.write("<br>"); document.write(x[i].getElementsByTagName("monthDate")[0].childNodes[0].nodeValue); document.write("/"); document.write(x[i].getElementsByTagName("dayDate")[0].childNodes[0].nodeValue); document.write("/"); document.write(x[i].getElementsByTagName("yearDate")[0].childNodes[0].nodeValue); document.write("<br>"); } } document.write("</div>"); </script> Can anyone help me with sorting this? So far I have it displaying only events that are occuring after the present date(currentDate). How would I go about displaying them so that the events displayed will be in the order of the earliest date displaying first? Thanks so much, Alex New problem, taken from another website I posted this on: Error: Code: Error ``user_node[0].firstChild is null'' [x-] in file ``*url removed*'', line 63, character 0. As you can see it says that the value is NULL although it shouldn't be... Here is the JavaScript which the error refers to: Code: function manageMessage() { if( receiveReq.readyState == 4 ) { // request complete var chat_div = document.getElementById( 'chat' ); var xmldoc = receiveReq.responseXML; var msg_nodes = xmldoc.getElementsByTagName( 'message' ); var n_msg = msg_nodes.length; for( i = 0; i < n_msg; i++ ) { alert(i + "," + n_msg); var user_node = msg_nodes[ i ].getElementsByTagName( 'user' ); var time_node = msg_nodes[ i ].getElementsByTagName( 'time' ); var text_node = msg_nodes[ i ].getElementsByTagName( 'text' ); alert(user_node[0].firstChild.nodeValue); //Display message chat_div.innerHTML += user_node[ 0 ].firstChild.nodeValue + "<br />"; chat_div.innerHTML += time_node[ 0 ].firstChild.nodeValue + "<br />"; chat_div.innerHTML += text_node[ 0 ].firstChild.nodeValue + "<br /><br />"; } } } And again, the whole script if you want it... Code: <script type="text/javascript"> //Credits// //Help: Greg (Gjslick) //Code example: http://www.dynamicajax.com/fr/AJAX_Driven_Web_Chat_Backend-271_290_291_296.html // Declare Global Variables var lastMess; var sendReq; var receiveReq; var mTimer; // Initialize Global Variables (on page load) function init() { lastMess = 0; sendReq = getXmlHttpRequestObject(); receiveReq = getXmlHttpRequestObject(); mTimer; } function getXmlHttpRequestObject() { if( window.XMLHttpRequest ) { return new XMLHttpRequest(); // code decent browsers like Firefox, Chrome, Opera, Safari. And IE7+... } else if( window.ActiveXObject ) { return new ActiveXObject( 'Microsoft.XMLHTTP' ); //Code for crap like IE } else { var statusEl = document.getElementById( 'p_status' ); statusEl.innerHTML = "Status: Could not create XmlHttpRequest Object, please upgrade your browser!"; return null; } } function getMessage() { //var url = "scripts/get_data.php?uid=<?=$uid?>&m=" + lastMess + "&chat=1"; var url = "testXMLdoc.xml"; receiveReq.open("GET",url,true); receiveReq.onreadystatechange = manageMessage; receiveReq.send( null ); lastMess++; } function manageMessage() { if( receiveReq.readyState == 4 ) { // request complete var chat_div = document.getElementById( 'chat' ); var xmldoc = receiveReq.responseXML; var msg_nodes = xmldoc.getElementsByTagName( 'message' ); var n_msg = msg_nodes.length; for( i = 0; i < n_msg; i++ ) { alert(i + "," + n_msg); var user_node = msg_nodes[ i ].getElementsByTagName( 'user' ); var time_node = msg_nodes[ i ].getElementsByTagName( 'time' ); var text_node = msg_nodes[ i ].getElementsByTagName( 'text' ); alert(user_node[0].firstChild.nodeValue); //Display message chat_div.innerHTML += user_node[ 0 ].firstChild.nodeValue + "<br />"; chat_div.innerHTML += time_node[ 0 ].firstChild.nodeValue + "<br />"; chat_div.innerHTML += text_node[ 0 ].firstChild.nodeValue + "<br /><br />"; } } } </script> And here is the XML file (test doc for now): Code: <?xml version="1.0" ?> <root> <message id="1"> <user></user> <text>testing stupid chat thing</text> <time>0000-00-00 00:00:00</time> </message> <message id="2"> <user></user> <text>dsadasfsdfsggsdgrgsdgfd</text> <time>2010-04-22 19:17:16</time> </message> </root> OK so say I have something like this: <TD> <p>Item A</p> <p>Item B</p> <p>Item C</p> <p>Item D</p> </TD> So TD would be the parent and all of the <P>'s would be childNodes. I want to be able to click on each child and have it alert me its index # relative to the parent node. So 'Item A' should alert "0", 'Item B' - "1", 'Item C' - "2", etc. Thanks in advance. |