Results 1 to 5 of 5

Thread: how do I grab <td> value if the <td> has no id?

  1. #1
    Join Date
    Jun 2008
    Posts
    16
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default how do I grab <td> value if the <td> has no id?

    I'm trying to compare one <td> to a large set of other <td>s, but how do I get at the <td> values if there are only class attributes, and no id attributes, associated with the large list of <td>s? Example HTML:

    HTML Code:
    <td class="filled rd2">Team 1</td>
    <td class="filled rd2">Team 2</td>
    
    <td id="myteam">Team 1</td>
    I want to find out if the value of the <td> with the id of "myteam" is the same as the value of ANY of the <td>s with the class of "rd2". Can I create a new array out of the <td>s that share the "rd2" class?

    Thanks for any help.

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Like this?:
    HTML Code:
    <table><tr><td class="hi">Hello</td><td>GoodBye!</td></tr></table>
    <script type="text/javascript">
    (function(){
      for(i = 0; (a = document.getElementsByTagName("td")[i]); i++){
        if(a.className = "hi"){
          a.innerHTML = "Hi!";
        }
      }
    })();
    </script>
    Jeremy | jfein.net

  3. #3
    Join Date
    Jun 2008
    Posts
    16
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default

    hey Nile, thanks, but I'm not sure. a few things:

    I don't want to change any HTML, I just want to compare. I have a NCAA bracket website, and if someone's bracket cell has the same value (i.e. the same winning team) as the correct bracket, then I want to add a class (class="correct") to that cell. So that's where the comparing comes in.

    Also, is .className a jQuery maneuver, or is it straight JS?

  4. #4
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    .className is "straight" js... I'll see what I can do with the comparison things.
    Jeremy | jfein.net

  5. #5
    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

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <style type="text/css">
    .correct {
     background-color: #ee8;
    }
    </style>
    </head>
    <body>
    <table><tr><td class="filled rd2">Team 1</td></tr>
    <tr><td class="filled rd2">Team 2</td></tr>
    
    <tr><td id="myteam">Team 1</td></tr></table>
    <script type="text/javascript">
    (function(){
      for(var i = 0, w = document.getElementById('myteam'), a; (a = document.getElementsByTagName("td")[i]); ++i){
        if(/\brd2\b/.test(a.className) && a.firstChild.nodeValue == w.firstChild.nodeValue){
          a.className += ' correct';
        }
      }
    })();
    </script>
    </body>
    </html>
    - John
    ________________________

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

  6. The Following User Says Thank You to jscheuer1 For This Useful Post:

    tk403 (03-02-2009)

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
  •