Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 6 of 6
Like Tree6Likes
  • 1 Post By TheGAME1264
  • 2 Post By Ronald Roe
  • 3 Post By sjondoe

Thread: for each loop in a table

  1. #1
    Junior Member
    Join Date
    Sep 2016
    Posts
    2
    Member #
    55302
    Liked
    3 times

    for each loop in a table

    Hi,

    I can't get figure this one out:

    I have a for each loop in which I want every td to have the title attribute with the value of each related th title. But its only prepends the first table row and not the rest.

    What am I missing?

    Codepen example
    Last edited by sjondoe; Sep 26th, 2016 at 02:45 PM. Reason: better description

  2.  

  3. #2
    Unpaid WDF Intern TheGAME1264's Avatar
    Join Date
    Dec 2002
    Location
    Not from USA
    Posts
    14,485
    Member #
    425
    Liked
    2783 times
    Code:
    var a = document.querySelectorAll('thead tr th');
    var b = document.querySelectorAll('tbody tr td');
    
    
    for (var i = 0; i < b.length; i++) {
      var c = a[i].innerHTML;
    
    
      b[i].setAttribute("title", c);
      b[5 + i].setAttribute("title", c);
      b[10 + i].setAttribute("title", c);
      b[15 + i].setAttribute("title", c);
      b[20 + i].setAttribute("title", c);
    }
    There are only 5 th elements, whereas you have 25 td elements (5 x 5). It will only do the first row because the first row contains the first five elements. This is a quicky-hacky fix...there is a better way to do this, but I'll leave that to you to play with.
    akssaz likes this.
    If I've helped you out in any way, please pay it forward. My wife and I are walking for Autism Speaks. Please donate, and thanks.

    If someone helped you out, be sure to "Like" their post and/or help them in kind. The "Like" link is on the bottom right of each post, beside the "Share" link.

    My stuff (well, some of it): My bowling alley site | Canadian Postal Code Info (beta)

  4. #3
    Senior Member Ronald Roe's Avatar
    Join Date
    Mar 2011
    Location
    Oklahoma City
    Posts
    3,141
    Member #
    27197
    Liked
    959 times
    The first thing you'll want to do is output what is stored in your variables. The best way to do that is to console.dir() and view it in the dev tools console.

    When I did that, I discovered that the problem is your loop. For each iteration of i, you're taking the innerHTML of a. There are only 5 indexes in a. There are 25 in b. So, once i > 5, there is no data to write.

    The fix is rather simple. You only need to use the modulus in your index call for the variable c. I've forked your pen and made this simple update. You can see it working here: A Pen by Ronald Roe
    Ron Roe
    Web Developer
    "If every app were designed using the same design template, oh wait...Bootstrap."

  5. #4
    Senior Member Ronald Roe's Avatar
    Join Date
    Mar 2011
    Location
    Oklahoma City
    Posts
    3,141
    Member #
    27197
    Liked
    959 times
    Quote Originally Posted by TheGAME1264 View Post
    This is a quicky-hacky fix...there is a better way to do this, but I'll leave that to you to play with.
    I got you bro.
    TheGAME1264 and akssaz like this.
    Ron Roe
    Web Developer
    "If every app were designed using the same design template, oh wait...Bootstrap."

  6. #5
    Unpaid WDF Intern TheGAME1264's Avatar
    Join Date
    Dec 2002
    Location
    Not from USA
    Posts
    14,485
    Member #
    425
    Liked
    2783 times
    I knew someone would, but I figured this was homework.
    If I've helped you out in any way, please pay it forward. My wife and I are walking for Autism Speaks. Please donate, and thanks.

    If someone helped you out, be sure to "Like" their post and/or help them in kind. The "Like" link is on the bottom right of each post, beside the "Share" link.

    My stuff (well, some of it): My bowling alley site | Canadian Postal Code Info (beta)

  7. #6
    Junior Member
    Join Date
    Sep 2016
    Posts
    2
    Member #
    55302
    Liked
    3 times

    Red face

    Thanks guys! I'm a newbie. You've really helped me understand the problem. This really helped me getting a better understanding of how loops work.

    @ Ronald,

    I didn't know you could use operators inside the index! Really an eye opener!

    With this in mind, to keep it scalable, I've done:
    Code:
    var c = a[i % a.length].innerHTML;
    Thanks again!
    Last edited by sjondoe; Sep 26th, 2016 at 04:37 PM.
    Ronald Roe, TheGAME1264 and akssaz like this.


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 08:48 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com