Selecting a particular row from a html table dynamically created

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • spooky
    New Member
    • Oct 2006
    • 27

    Selecting a particular row from a html table dynamically created

    Hi,

    I have a html table that has its rows dynamically created. My first column contains radio buttons. I want to get the row for which the radio button is selected.


    Spooky
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Show your code. What have you tried so far?

    Comment

    • jcumoletti
      New Member
      • Apr 2008
      • 6

      #3
      here is an example that will work in both firefox and IE. this should give you everything you would want to know about the radio button that was clicked

      Code:
      function radioClicked(btn){
        var tbl,cell,row,rowIndex,cellIndex,tblID;
        cell = btn.parentNode;
        row= cell.parentNode;
        tbl=row.parentNode;
        while(tbl.nodeName!="TABLE")//this is incase the table has a body     tbl=tbl.parentNode;
        rowIndex = row.rowIndex; //starts at 0 so we will add 1 
        rowIndex++;
        cellIndex = cell.cellIndex; //starts at 0 so we will add 1 
        cellIndex++;
        tblID = tbl.id;
        alert("A radio button has been clicked in row " + rowIndex + " cell " +  cellIndex + " in table whos id is " + tblID);
      }
      [HTML]
      <table id='Table1' border=1>
      <tr>
      <td><input name='btn' onclick='radioC licked(this)' type='radio'/>radio button</td>
      <td><input name='btn' onclick='radioC licked(this)' type='radio'/>radio button</td>
      </tr><Tr>
      <td><input name='btn' onclick='radioC licked(this)' type='radio'/>radio button</td>
      <td><input name='btn' onclick='radioC licked(this)' type='radio'/>radio button</td>
      </tr><tr>
      <td><input name='btn' onclick='radioC licked(this)' type='radio'/>radio button</td>
      <td><input name='btn' onclick='radioC licked(this)' type='radio'/>radio button</td>
      </tr>
      </table><br>
      <table id='Table2' border=1>
      <tr>
      <td><input name='btn' onclick='radioC licked(this)' type='radio'/>radio button</td>
      <td><input name='btn' onclick='radioC licked(this)' type='radio'/>radio button</td>
      </tr><Tr>
      <td><input name='btn' onclick='radioC licked(this)' type='radio'/>radio button</td>
      <td><input name='btn' onclick='radioC licked(this)' type='radio'/>radio button</td>
      </tr><tr>
      <td><input name='btn' onclick='radioC licked(this)' type='radio'/>radio button</td>
      <td><input name='btn' onclick='radioC licked(this)' type='radio'/>radio button</td>
      </tr>
      </table>
      [/HTML]

      Comment

      Working...