Undefined first element in array javascript -
Undefined first element in array javascript -
i new javascript
/ajax
if obvious please don't harsh.
as far know(i may wrong) have javascript reads xml file , extracts between time
tags. problem is, no matter how code first list item output undefined. able explain why happening , possible solution please ? if else massively wrong please allow me know
i have tried
if(x[0] !== 'undefined'){txt=txt + "<li class=\"result\">" + x[i].firstchild.nodevalue + "</li>";} if(x !== 'undefined'){txt=txt + "<li class=\"result\">" + x[i].firstchild.nodevalue + "</li>";} if(x[0].firstchild.nodevalue !== 'undefined'){txt=txt + "<li class=\"result\">" + x[i].firstchild.nodevalue + "</li>";} if(x[i].firstchild.nodevalue !== 'undefined'){txt=txt + "<li class=\"result\">" + x[i].firstchild.nodevalue + "</li>";} if(x[i].firstchild.nodevalue !== ''){txt=txt + "<li class=\"result\">" + x[i].firstchild.nodevalue + "</li>";}
also tried assigning first element
x[0]="n";
heres total html split pieces. have reduced relevant parts.
style , start<!doctype html> <html> <head> <style> body { text-align: center; height: 1100px; background-color: #eaeaea } ul.menu { list-style-type: none;; padding: 0px; width: 205px; height: 500px; margin: 0px; overflow-y: scroll; overflow-x: hidden; } ul.menu::-webkit-scrollbar { display: none; } #button-container { list-style-type: none;; padding: 0px; width: 205px; height: 500px; margin: 0px; overflow-y: scroll; overflow-x: hidden; } #button-container::-webkit-scrollbar { display: none; } times { position: absolute; display: block; width: 35%; height: 100%; left:0px; border-right: 2px solid #e5e5e5; } li.result { display: block; font-family: "lucida sans"; font-size: 12px; font-weight: bold; width:164px; padding-top: 30px; padding-bottom: 30px; color: #5d5d5d; text-align: center; text-decoration: none; width:100%; border-bottom: 1px solid #cccccc; border-top: 1px solid #cccccc; background-color: #eeeeee; } li.result:hover { background-color: #6bb9f0; color: #fcfcfc; } .btn { position:relative; display:block; border:none; font-family: "lucida sans"; font-size: 10px; font-weight: bold; width:204px; padding-top: 20px; padding-bottom: 20px; color: #5d5d5d; background-color: #fafafa; text-align: left; text-decoration: none; } .btn:hover{ background-color: #5cd8cd; color: #fcfcfc; } </style>
script part <script type="text/javascript"> function myfunction(url) { var xmlhttp; var txt,x,i; if (window.xmlhttprequest) {// code ie7+, firefox, chrome, opera, safari xmlhttp=new xmlhttprequest(); } else {// code ie6, ie5 xmlhttp=new activexobject("microsoft.xmlhttp"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readystate==4 && xmlhttp.status==200) { x=xmlhttp.responsexml.documentelement.getelementsbytagname("time"); (i=0;i<x.length;i++) { txt=txt + "<li class=\"result\">" + x[i].firstchild.nodevalue + "</li>"; } document.getelementbyid('bob').innerhtml=txt; } } xmlhttp.open("get",url,true); xmlhttp.send(); } </script> </head>
body (stripped downwards this).
<body> <button class="btn" style="text-align:center;" onclick="myfunction('test.xml')">all</button> <times> <ul id="bob" class="menu" style="width:100%;height:91%;position:relative;top:29px;border-top: 1px solid #cccccc;"> </ul> </times> </body> </html>
edit:
xml<root> <time>mon sep 29 11:20:00 bst 2014</time> <time>mon sep 29 11:20:15 bst 2014</time> <time>mon sep 29 11:20:30 bst 2014</time> <time>mon sep 29 11:20:45 bst 2014</time> <time>mon sep 29 11:21:00 bst 2014</time> <time>mon sep 29 11:21:15 bst 2014</time> <time>mon sep 29 11:21:30 bst 2014</time> <time>mon sep 29 11:21:45 bst 2014</time> <time>mon sep 29 11:22:00 bst 2014</time> <time>mon sep 29 11:22:15 bst 2014</time> <time>mon sep 29 11:22:30 bst 2014</time> <time>mon sep 29 11:22:45 bst 2014</time> <time>mon sep 29 11:23:00 bst 2014</time> <time>mon sep 29 11:23:15 bst 2014</time> <time>mon sep 29 11:23:30 bst 2014</time> <time>mon sep 29 11:23:45 bst 2014</time> <time>mon sep 29 11:24:00 bst 2014</time> <time>mon sep 29 11:24:15 bst 2014</time> <time>mon sep 29 11:24:30 bst 2014</time> <time>mon sep 29 11:24:45 bst 2014</time> <time>mon sep 29 11:25:00 bst 2014</time> <time>mon sep 29 11:25:15 bst 2014</time> <time>mon sep 29 11:25:30 bst 2014</time> <time>mon sep 29 11:25:45 bst 2014</time> <time>mon sep 29 11:26:00 bst 2014</time> <time>mon sep 29 11:26:15 bst 2014</time> <time>mon sep 29 11:26:30 bst 2014</time> <time>mon sep 29 11:26:45 bst 2014</time> <time>mon sep 29 11:27:00 bst 2014</time> <time>mon sep 29 11:27:15 bst 2014</time> <time>mon sep 29 11:27:30 bst 2014</time> <time>mon sep 29 11:27:45 bst 2014</time> </root>
thanks help
if else has problem, guessing unlikely silly mistake
the undefined list item due txt
beingness undefined when gets added its(previously undefined) self in line
txt=txt + "<li class=\"result\">" + x[i].firstchild.nodevalue + "</li>";
adding
txt=''
prior line fixed problem.
javascript arrays ajax
Comments
Post a Comment