Javascript – Internet Explorer 7 – Javascript ‘undefined’ not testing

cross-browserfirefoxinternet-explorerinternet-explorer-7javascript

I'm having trouble with some JS in IE7. I'm testing to see if a certain object has a className (its possibly an HTMLElement object from the DOM) assigned.

Now, testing the page in Firefox tells me that Yes, the variable is undefined (all my tests below do the Alert().

In IE, none of the tests pass, the variable gets assigned on the last IF statement, and during the last Alert() IE chucks an "className is null or not an object" error, based on the fn_note.className statement.

Here's the code:

        var fn_note;
        var kids = area.childNodes;
        for (var l = 0; l < kids.length; l++){
            //DEBUG check if the found var exists
            if (kids[l].className == null){
                //then the className var doens't exist
                alert ('the classsname for the following var is null: --'+kids[l]+'--');
            }
            if (kids[l].className == undefined){
                //then the className var doens't exist
                alert ('the classsname for the following var is undefined: --'+kids[l]+'--');
            }                    
            if (kids[l].className == ''){
                //then the className var doens't exist
                alert ('the classsname for the following var is an empty string: --'+kids[l]+'--');
            }
            if (typeof kids[l].className === 'undefined'){
                //then the className var doens't exist
                alert ('the classsname for the following var is NEW TYPEOF TEST: --'+kids[l]+'--');
            }                      

            if (kids[l].className == 'fn-note') { /* (/fn-note$/).test(kids[l].className) IE doesn't really like regex. por supuesto */
                //we have found the div we want to hide
                fn_note = kids[l];                       
            }
        }                    
        alert('the clicked on className is '+area.className+'name of the found div is '+fn_note.className); 

Please let me know what I am doing wrong. I know its probably something basic but I just can't see it ATM.

Thanks in advance.

Best Solution

I think that what you get from the property is not a string, but an object of the type 'undefined'. Try this:

if (typeof(kids[l].className) == 'undefined') {