0

Below is a portion of code I'm using to get the href or src of an image of known class or id. The console.log() returns null, even though it is in an if statement checking that the attribute later used isn't null. Of course, trying to get href or src of null doesn't end well.

for(var i = 0 ; (i < sitelist[site].img_id.length) && (img === undefined) ; i++)
{
    if(document.getElementById(sitelist[site].img_id[i]) !== undefined)
    {
        if(document.getElementById(sitelist[site].img_id[i]) !== null)
        {
            console.log(document.getElementById(sitelist[site].img_class[i]));
            if(document.getElementById(sitelist[site].img_class[i]).href !== undefined)
            {
                img =   document.getElementById(sitelist[site].img_class[i]).href;
            }
            if(document.getElementById(sitelist[site].img_class[i]).src !== undefined)
            {
                img =   document.getElementById(sitelist[site].img_class[i]).src;
            }
        }
    }
}

Is there a specific way of checking if something is null, or is the problem elsewhere?

Caramel Truffle
  • 257
  • 1
  • 4
  • 15

4 Answers4

2

You can just try the below code.

Replace

if (document.getElementById(sitelist[site].img_id[i]) !== undefined) {
    if (document.getElementById(sitelist[site].img_id[i]) !== null) {
        // ...
    }
}

with

if (document.getElementById(sitelist[site].img_id[i])) {
    // process here
}

You are checking null and undefined continuously in next statement. You can just try with element only.

Paul S.
  • 64,864
  • 9
  • 122
  • 138
SivaRajini
  • 7,225
  • 21
  • 81
  • 128
  • http://stackoverflow.com/questions/2559318/how-to-check-for-undefined-or-null-variable-in-javascript – SivaRajini May 22 '13 at 12:54
  • The problem wasn't on the structure but on the variables used. The test was on img_id and img_class was used. Your answer is perfectly true, but the problem wasn't there. – Caramel Truffle May 22 '13 at 13:13
1

Replace

   id[i]) !== undefined

with

   id[i]) != undefined 

in all statements

Prasath K
  • 4,950
  • 7
  • 23
  • 35
PSR
  • 39,804
  • 41
  • 111
  • 151
1

To test if the element is found, simply use

var elem = document.getElementById(sitelist[site].img_id[i]);
if (elem) {
   // proceed from there

Note that I also declared a variable. Repeating your document.getElementById(sitelist[site].img_id[i]) makes your code hard to read (and thus debug and maintain).

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
0

Your null and undefined tests are correct, though longer than needed.

Your actual issue is that you're testing for .img_id[i], but then using .img_class[i].

Here's a more concise version of your existing code.

for(var i = 0 ; (i < sitelist[site].img_id.length) && (img === undefined) ; i++) { 

// Here you're testing the `img_id[i]`
    var img_id = document.getElementById(sitelist[site].img_id[i]);

    if(img_id != null) {

// Here you're trying to use a different element
        var img_clss = document.getElementById(sitelist[site].img_class[i]);
        console.log(img_clss);

        if(img_clss.href !== undefined) {
            img =   img_clss.href;
        }
        if(img_clss.src !== undefined) {
            img = img_clss.src;
        }
    }
}

Unless the values are the same, testing for one isn't going to tell you if a different one exists.

I don't know what the intent is, but perhaps you just need to change the element inside the if.

for(var i = 0 ; (i < sitelist[site].img_id.length) && (img === undefined) ; i++) { 
    // Here you're testing the `img_id[i]`
    var img_id = document.getElementById(sitelist[site].img_id[i]);

    if(img_id != null) {
        // Now we're using the same element that we tested!
        console.log(img_id);

        if(img_id.href !== undefined) {
            img = img_id.href;
        }
        if(img_id.src !== undefined) {
            img = img_id.src;
        }
    }
}
  • Thank you! The real problem was truly that I was checking img_id then using img_class. Shame on me. I will ameliorate its readability and compactness though. – Caramel Truffle May 22 '13 at 13:10