Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Dec 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    getting a particular TD content of table

    hi all,
    I am trying to alert the td content of a table . I pass the company Id as the parameter and need to alert the company name.I am able to alert only the first company.What needs to be done.?Please help
    Code:
    <html>
    <head>
    <script type="text/javascript">
    function displayID(id)
    {
    var match=id.trim();
    alert(match);
    var table = document.getElementById('MyTable');
            for (var r = 0, n = table.rows.length; r < n; r++) {
                for (var c = 0, m = table.rows[r].cells.length; c < m; c++) {
                    alert(table.rows[r].cells[c].innerHTML);
    		if(table.rows[r].cells[c].innerHTML==match){
    alert("Found!");
    alert(table.rows[r].cells[c-1].innerHTML)
    }
                }//for c
            }//for r
    }
    </script>
    </head>
    <body>
    <table border="1" id="MyTable">
    <tr>
    <td> </td>
    <td>Company Name </td>
    <td> Company Id </td>
    <td>Status </td>
    </tr>
    <tr>
    <td><input type="radio" name="radio_newcontact" id="radio_newcontact" value="ABC" onclick="displayID(this.value)"/></td>
    <td id="ABC tradin Co">ABC Trading Co</td>
    <td id="ABC">ABC</td>
    <td>Active</td>
    </tr>
    <tr>
    <td><input type="radio" name="radio_newcontact" id="radio_newcontact" value="DEF" onclick="displayID(this.value);"></td>
    <td id="DEF Co">DEF Co</td>
    <td id="DEF"> DEF</td>
    <td>Active</td>
    </tr>
    <tr>
    <td><input type="radio" name="radio_newcontact" id="radio_newcontact" value="GHI" onclick="displayID(this.value);"></td>
    <td id="GHI LTD">GHI LTD</td>
    <td id="GHI"> GHI</td>
    <td>Active</td>
    </tr>
    </table>
    </body>
    </html>

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,065
    Thanks
    36
    Thanked 497 Times in 491 Posts

    Lightbulb

    Part of your problem is that you have duplicated ID values.
    ID values must be unique. Otherwise getElementById() gets very confused.

    Also your onclick function calls a loop that looks at each cell of the table.
    Why?

    What is it exactly that you want to alert? Just the Company name or something else?
    If it is just the ID then this should work...
    Code:
    onclick="alert(this.id)"
    or use "alert(this.value)" if you don't really mean to "displayID"

  • #3
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    849
    Thanks
    172
    Thanked 92 Times in 92 Posts
    Hi

    note you had spaces in your table so your match would fail
    <td id="DEF"> DEF</td>

    Also you cannot have duplicated ID's -- ID's are unique -- no two ID's can have the same name on a page as jmrker has mentioned.


    try this and see if it helps

    Code:
    <html>
    <head>
    <!-- http://www.codingforums.com/showthread.php?t=267923 -->
    <script type="text/javascript">
    function displayID(id)
    {
      var match=id.trim();
      alert("looking to match  "+match);
      var table = document.getElementById('MyTable');
      for (var r = 0; r < table.rows.length; r++) {
        for (var c = 0; c < table.rows[r].cells.length; c++) {
          //alert(table.rows[r].cells[c].innerHTML);
          if(table.rows[r].cells[c].innerHTML==match){
            alert("Found!");
            alert(table.rows[r].cells[c-1].innerHTML);
          }   
        }//for c
      }//for r
    }
    </script>
    </head>
    <body>
    <table border="1" id="MyTable">
    <tr>
    <td></td>
    <td>Company Name </td>
    <td> Company Id </td>
    <td>Status </td>
    </tr>
    <tr>
    <td><input type="radio" name="radio_newcontact" id="radio_newcontact0" value="ABC" onclick="displayID(this.value)"/></td>
    <td id="ABC tradin Co">ABC Trading Co</td>
    <td id="ABC">ABC</td>
    <td>Active</td>
    </tr>
    <tr>
    <td><input type="radio" name="radio_newcontact" id="radio_newcontact1" value="DEF" onclick="displayID(this.value);"></td>
    <td id="DEF Co">DEF Co</td>
    <td id="DEF">DEF</td>
    <td>Active</td>
    </tr>
    <tr>
    <td><input type="radio" name="radio_newcontact" id="radio_newcontact2" value="GHI" onclick="displayID(this.value);"></td>
    <td id="GHI LTD">GHI LTD</td>
    <td id="GHI">GHI</td>
    <td>Active</td>
    </tr>
    </table>
    </body>
    </html>
    LT
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.


  •  

    Tags for this Thread

    Posting Permissions

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