SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Nov 2005
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript Array Dealing with .length

    Say I have this looping for statement "And it works fine":

    for (i=1; i<10; i++) {
    obj = document.getElementById("rTable" + i);
    if (obj == null) break;
    if (obj.offsetHeight >= 268) {
    obj.style.height="268px";
    }
    }

    The problem is that I don't want to use i<10; because I don't know the exact number.
    I want to use the length of the array. So how do I set that up?

    Thanks,

    Judd

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Portsmouth UK
    Posts
    1,499
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <myarray.length

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Location
    London, UK
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Count it by using getElementsByTagName (or possibly document.all[] but that's maybe slower). Untested:
    Code:
    var mytables =new Array();
    var tl = document.getElementsByTagName('table');
    for (e in tl) {
       if (tl[e].getAttribute('id') && tl[e].getAttribute('id').indexOf('rTable') > -1)
          mytables.push(tl[e]);
    }
    Then you'll be able to do
    Code:
    for (var i=0, len=mytables.length; i<len; i++)

  4. #4
    SitePoint Member
    Join Date
    Nov 2005
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    document.getElementById("rTable") is actually a div. I'm sorry for the Confusion.

  5. #5
    SitePoint Addict dek's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    for (var obj,i=1; (obj = document.getElementById("rTable" + i)); i++) {
       if (obj.offsetHeight >= 268) obj.style.height="268px";
    }
    Only dead fish go with the flow

  6. #6
    SitePoint Member
    Join Date
    Nov 2005
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile

    Alright man, thanks a bunch it worked!

    Just for learning sake, why did you take out?

    if (obj == null) break;

    Thanks a bunch!

  7. #7
    SitePoint Addict dek's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Because it wasn't needed. There's a nice explanation for this in the 'Useful tips and Tricks' thread - but in a nutshell, the statement:

    Code:
    if (obj = document.getElementById("rTable" + i)) { }
    will succeed if document.getElementById("rTable" + i) returns a valid object, and will fail if it returns null - hence ending the loop. It's a really handy trick.

    Glad it helped.
    Only dead fish go with the flow

  8. #8
    SitePoint Member
    Join Date
    Nov 2005
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    I C, thanks a BUNCH!


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
  •