[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1375
  • Last Modified:

How to get an rowindex on a table cell click

Hi,

I have a html table defined as:

<table>

                 <tr style="color:Black;font-size:8pt;">
                 <td nowrap="nowrap" style="background-color:#E9E9FF;">
                     <input id="dgDOCGrid__ctl2_DeleteThis" type="checkbox" name="dgDOCGrid:_ctl2:DeleteThis" />
                  </td><td>
                     <input name="dgDOCGrid:_ctl2:txtFunc" type="text" id="dgDOCGrid__ctl2_txtFunc" />
                  </td><td>
                     <input name="dgDOCGrid:_ctl2:txtASPx" type="text" value="iol.asp" id="dgDOCGrid__ctl2_txtASPx" />
                  </td><td>
                     <input name="dgDOCGrid:_ctl2:txtTableName" type="text" value="ac" id="dgDOCGrid__ctl2_txtTableName" />
                  </td><td>
                     <input name="dgDOCGrid:_ctl2:txtFieldName" type="text" value="ac" id="dgDOCGrid__ctl2_txtFieldName" disabled="disabled" />
                  </td><td>
                     <input id="dgDOCGrid__ctl2_chkDelete" type="checkbox" name="dgDOCGrid:_ctl2:chkDelete" />
                  </td><td>
                     <input id="dgDOCGrid__ctl2_chkInsert" type="checkbox" name="dgDOCGrid:_ctl2:chkInsert" />
                  </td><td>
                     <input id="dgDOCGrid__ctl2_chkUpdate" type="checkbox" name="dgDOCGrid:_ctl2:chkUpdate" checked="checked" />
                  </td><td>
                     <input id="dgDOCGrid__ctl2_chkRead" type="checkbox" name="dgDOCGrid:_ctl2:chkRead" checked="checked" />
                  </td><td>
                     <a id=1800 href=javascript:getComment('1800') class="listitem">...</a>
                     <input name="dgDOCGrid:_ctl2:hdnComment" id="dgDOCGrid__ctl2_hdnComment" type="hidden" />
                  </td>
      </tr>
                <tr style="color:Black;background-color:#FFFFCC;font-size:8pt;">
        <td nowrap="nowrap" style="background-color:#E9E9FF;">
                     <input id="dgDOCGrid__ctl3_DeleteThis" type="checkbox" name="dgDOCGrid:_ctl3:DeleteThis" />
                  </td><td>
                     <input name="dgDOCGrid:_ctl3:txtFunc" type="text" id="dgDOCGrid__ctl3_txtFunc" />
                  </td><td>
                     <input name="dgDOCGrid:_ctl3:txtASPx" type="text" value="iol.asp" id="dgDOCGrid__ctl3_txtASPx" />
                  </td><td>
                     <input name="dgDOCGrid:_ctl3:txtTableName" type="text" value="ac" id="dgDOCGrid__ctl3_txtTableName" />
                  </td><td>
                     <input name="dgDOCGrid:_ctl3:txtFieldName" type="text" value="descrip" id="dgDOCGrid__ctl3_txtFieldName" disabled="disabled" />
                  </td><td>
                     <input id="dgDOCGrid__ctl3_chkDelete" type="checkbox" name="dgDOCGrid:_ctl3:chkDelete" />
                  </td><td>
                     <input id="dgDOCGrid__ctl3_chkInsert" type="checkbox" name="dgDOCGrid:_ctl3:chkInsert" />
                  </td><td>
                     <input id="dgDOCGrid__ctl3_chkUpdate" type="checkbox" name="dgDOCGrid:_ctl3:chkUpdate" />
                  </td><td>
                     <input id="dgDOCGrid__ctl3_chkRead" type="checkbox" name="dgDOCGrid:_ctl3:chkRead" checked="checked" />
                  </td><td>
                     <a id=1802 href=javascript:getComment('1802') class="listitem">...</a>
                     <input name="dgDOCGrid:_ctl3:hdnComment" id="dgDOCGrid__ctl3_hdnComment" type="hidden" />
                  </td>
              </tr>
</table>

When the user click on the hyper link of <a href> with an id=1802, how do I know that is a second row in the javascript getComment('1802')?

Many thanks
0
tnova1
Asked:
tnova1
  • 3
  • 3
1 Solution
 
daohailamCommented:
You should do like this:

<script>
function getComment(link) {
      var row = link.parentNode.parentNode;
      var table = row.parentNode;
      for (var i = 0; i < table.rows.length; i++)
            if (row == table.rows[i]) {
                  alert(link.id + ': ' + i);
                  return i;
            }
      return -1;
}
</script>

<a id="1800" href="#" onclick="getComment(this)" class="listitem">...</a>
...
<a id="1802" href="#" onclick="getComment(this)" class="listitem">...</a>
0
 
tnova1Author Commented:
Hi Dao,

the link.parentNode is not working. It is not un object

Diane
0
 
tnova1Author Commented:
Hi Dao,

Off course, I like your code because I only need one for loop.  But the link.parentNode is not working, it return an unidentify object.  I think this is a DOM object, not html table.
Here is my code:
         function getComment(varCount)
         {
          //IF COMMENT EXISTS, DISPLAY IN EDITABLE POPUP - IF NOT, DISPLAY EMPTY POPUP - THEN SAVE THE INPUT TO THE HIDDEN INPUT
            var strControlName;
            var control;
            var table = document.getElementById("dgDOCGrid");
               varCount = "getComment('" + varCount + "')";
               var rows =  table.getElementsByTagName("tr");
               for (i = 0; i < rows.length + 1; i++)
               {
                  var cols = rows[i].getElementsByTagName("td");
                  for (n = 0; n < cols.length; n++)
                  {
                     strHTML = cols[n].innerHTML;
                     if (strHTML != null)
                        if (strHTML.indexOf(varCount) > 0)
                        {
                           strControlName = "dgDOCGrid__ctl" + i + "_hdnComment";
                           control = document.getElementById(strControlName);
                            if (control != null)                           
                                break;
                          }
                  }
                  if (control != null)
                  break;
               }

Let me know if you know how to make the parentNode work.
Thanks,
Diane
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
daohailamCommented:
Hmm, strange, it works well here on both IE6 & FF. Anyway, try this one:

1. Is it ok, if we change from this one
                  <td>
                     <a id=1802 href=javascript:getComment('1802') class="listitem">...</a>
                     <input name="dgDOCGrid:_ctl3:hdnComment" id="dgDOCGrid__ctl3_hdnComment" type="hidden" />
                  </td>
   to this one:
                  <td id="1802" onclick="getComment(this)" class="listitem">...
                     <input name="dgDOCGrid:_ctl3:hdnComment" id="dgDOCGrid__ctl3_hdnComment" type="hidden" />
                  </td>

2. If ok, try this script:

<script>
function getComment(cell) {
     var row = cell.parentNode;
     var table = row.parentNode;
     for (var i = 0; i < table.rows.length; i++)
          if (row == table.rows[i]) {
               alert(link.id + ': ' + i);
               return i;
          }
     return -1;
}
</script>

Still not sure, if parentNode does not exist.
0
 
tnova1Author Commented:
No, as I already said that parentNode is not a html.table property.  Off course I did change getComment('1802') to getComment(this).

The IE is version 6.  

0
 
daohailamCommented:
Ok, just try to simplify your code a litte bit.  Because each row just has only 1 <a>, we can try this code:

function getComment(varCount)
{
    //IF COMMENT EXISTS, DISPLAY IN EDITABLE POPUP - IF NOT, DISPLAY EMPTY POPUP - THEN SAVE THE INPUT TO THE HIDDEN INPUT
    var strControlName;
    var control;
    var table = document.getElementById("dgDOCGrid");
    var rows = table.getElementsByTagName("tr"); //  <==  We can use "table.rows" here
    for (i = 0; i < rows.length + 1; i++) {
       var a = rows[i].getElementsByTagName("a");
       if (a.length && a[0].id == varCount) {
           strControlName = "dgDOCGrid__ctl" + i + "_hdnComment";
           control = document.getElementById(strControlName);
           break;
        }
    }
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now