Results 1 to 5 of 5

Thread: Element is null?

  1. #1
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default Element is null?

    Is this the incorrect syntax? I'm trying to find which in a series of 5 IDs is not set.

    Code:
    for (i=1;i<=6;i++) {
    	if (document.getElementById('inactive' + i) === null) {
    		alert("it is" +i);
    	}
    }
    Last edited by bluewalrus; 11-22-2010 at 12:17 AM.
    Corrections to my coding/thoughts welcome.

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    you mean "inactive1", "inactive2", etc.? Just "if" works:
    Code:
    for (i=1; i<6; i++) {
      if(!document.getElementById('inactive' + i){ alert('it is ' + i); }
    }

  3. The Following User Says Thank You to traq For This Useful Post:

    bluewalrus (11-19-2010)

  4. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    You should declare the variable i. In this context ++i (pre-increment) is more efficient than i++ (post-increment). Also, and Adrian corrects this in his code but doesn't mention it, there is a difference between +i and + i. The former means "positive i" and is roughly equivalent to parseFloat(i) or 1 * i. In the context of the code, this would be a syntax error. The latter means "plus i" (in this context "concat the string 'it is' and the value of i). So that's the big no no in the original code. In the alert I think you want a space after "is". If we don't want a bunch of alerts in cases where - say only inactive1 exists, we should return from the function. So we could then have:

    Code:
    for (var i = 1; i <= 6; ++i) {
    	if (document.getElementById('inactive' + i) === null) {
    		alert("it is " + i);
    		return;
    	}
    }
    But, as Adrian points out, such a strict test of the element isn't required. What he doesn't mention is that it might fail in some cases (though I'm not certain of that, if it doesn't it's preferred for efficiency's sake). In any case his solution works:

    Code:
    for (var i = 1; i <= 6; ++i) {
    	if (!document.getElementById('inactive' + i)){
    		alert("it is " + i);
    		return;
    	}
    }
    Note: Looking back over this I realized that this code might not be in a function, or even if it is we might not want the function to return just because it 'finds' a missing element. If that's the case we can substitute break; for return; - That way it will break out of the loop regardless of whether or not the loop is inside a function or not and continue processing after the closing brace of the for statement.
    Last edited by jscheuer1; 11-20-2010 at 05:28 AM. Reason: English usage, later add note
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. The Following 2 Users Say Thank You to jscheuer1 For This Useful Post:

    bluewalrus (11-19-2010),traq (11-19-2010)

  6. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    thanks John, javascript isn't my strong suit (I'm really just now starting to get comfortable) - what I offered was based solely on testing and finding out it works The things I "didn't mention," I didn't know. Thanks for expounding!

  7. #5
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Yup that did it. Thanks.
    Corrections to my coding/thoughts welcome.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •