Undefined issues

Hi :slight_smile:

I’m hoping for a little bit of help. I’m bringing in a CSV file via Javascript/AJAX and then attempting to create an unordered list.

The AJAX bit works just fine, however building the list is proving an issue. Below is the relevant code:


unction makeList() {
    if (xmlhttp.readyState==4) { 
        if (xmlhttp.status==200) { 
            var tmpArr=xmlhttp.responseText.split('\
');
            var out='<ul>';
            var tmp;
            var val;
            var txt;
            for (var idx=0;idx<tmpArr.length;idx++) {
                tmp=tmpArr[idx].split(',');
				txt = tmp[1];
                val = tmp[0];
				if(txt.charAt(0)=="I") {
				out += '<li><a href="#">'+txt+'</a></li>';
				}else{
					//do nothing. Just in place whilst testing
				}
            }
            out += '</ul>';
            document.getElementById('prodMenuItem').innerHTML=out;
        }
    }

}

As you can see I am attempting to bring out items from the CSV file that begin with “I”. Everytime I do this I get:

txt is undefined

which errors out at the line

if(txt.charAt(0)=="I") {

But bizarrely if I do:

alert(txt);

AFTER the line my debugger says the code breaks at, then all the 'I’s do indeed get alerted out to the screen.

If anyone has any idea whats going on and how to approach fixing this I’d be very grateful :slight_smile:

It would appear tmp[1] is undefined, so assigning it to txt makes txt also undefined.

Can you give an example responseText ?

Hi :slight_smile: thanks for your help.

I don;t understand how a var can be undefined and yet, as I said, when called to an alert box, display the right set of results.

I tried putting in a set of

if(txt == undefined){
alert("arrgghhhh");
}

at varying points and can see that it gains definition right before the line

txt = tmp[1];

But then my debugger (venkman) swears that it (txt) is not defined and I get no results at all.

– text removed, double post… –

And when you try by converting al txt to strings, like


 if(String(txt).charAt(0)=="I") {
   // ...
 }

Does that work?

Thats got it - thanks very much :slight_smile: