6

I need your help.

I have the following table as detailed below. What I would like to do is to click on one of the rows in the table (headers excluded) and obtain the column values of the selected row.

How do I populate the textboxes with the information from the selected row in the table?

No libraries please.

<!DOCTYPE html>

<html>

<head>

</head>

<body>


<table id="myTable" cellspacing="1">             
    <thead>> 
        <tr> 
            <th>first name</th> 
            <th>last name</th> 
            <th>age</th> 
            <th>total</th> 
            <th>discount</th> 
            <th>diff</th> 
        </tr> 
    </thead> 
    <tbody> 
        <tr> 
            <td>peter</td> 
            <td>parker</td> 
            <td>28</td> 
            <td>9.99</td> 
            <td>20.3%</td> 
            <td>+3</td> 
        </tr> 
        <tr> 
            <td>john</td> 
            <td>hood</td> 
            <td>33</td> 
            <td>19.99</td> 
            <td>25.1%</td> 
            <td>-7</td> 
        </tr> 
        <tr> 
            <td>clark</td> 
            <td>kent</td> 
            <td>18</td> 
            <td>15.89</td> 
            <td>44.2%</td> 
            <td>-15</td> 
        </tr> 
        <tr> 
            <td>bruce</td> 
            <td>almighty</td> 
            <td>45</td> 
            <td>153.19</td> 
            <td>44%</td> 
            <td>+19</td> 
        </tr> 
        <tr> 
            <td>bruce</td> 
            <td>evans</td> 
            <td>56</td> 
            <td>153.19</td> 
            <td>23%</td> 
            <td>+9</td> 
        </tr> 
    </tbody> 
</table>

Firstname is:<input type="text" id="firstname" />
<br>
Lastname is:<input type="text" id="lastname" />
<br>
Age is:<input type="text" id="age" />
<br>
Total is:<input type="text" id="total" />
<br>
Discount is:<input type="text" id="discount" />
<br>
Diff is:<input type="text" id="diff" />
</body>

</html>
Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120
John Smith
  • 1,479
  • 9
  • 32
  • 50

4 Answers4

10

Here is one example how to use event delegation in javascript (it's especially convenient for dynamically added content) . The event handler is attached to TABLE element's onclick attribute:

function run(){
    document.getElementById('myTable').onclick = function(event){
        event = event || window.event; //for IE8 backward compatibility
        var target = event.target || event.srcElement; //for IE8 backward compatibility
        while (target && target.nodeName != 'TR') {
            target = target.parentElement;
        }
        var cells = target.cells; //cells collection
        //var cells = target.getElementsByTagName('td'); //alternative
        if (!cells.length || target.parentNode.nodeName == 'THEAD') { // if clicked row is within thead
            return;
        }
        var f1 = document.getElementById('firstname');
        var f2 = document.getElementById('lastname');
        var f3 = document.getElementById('age');
        var f4 = document.getElementById('total');
        var f5 = document.getElementById('discount');
        var f6 = document.getElementById('diff');
        f1.value = cells[0].innerHTML;
        f2.value = cells[1].innerHTML;
        f3.value = cells[2].innerHTML;
        f4.value = cells[3].innerHTML;
        f5.value = cells[4].innerHTML;
        f6.value = cells[5].innerHTML;
    }
}

JSFIDDLE: code / result page

The alternative solution, where the event handler is directly attached to each TR element can look like this:

function run() {
    var t = document.getElementById('myTable');
    var rows = t.rows; //rows collection - https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableElement
    for (var i=0; i<rows.length; i++) {
        rows[i].onclick = function () {
            if (this.parentNode.nodeName == 'THEAD') {
                return;
            }
            var cells = this.cells; //cells collection
            var f1 = document.getElementById('firstname');
            var f2 = document.getElementById('lastname');
            var f3 = document.getElementById('age');
            var f4 = document.getElementById('total');
            var f5 = document.getElementById('discount');
            var f6 = document.getElementById('diff');
            f1.value = cells[0].innerHTML;
            f2.value = cells[1].innerHTML;
            f3.value = cells[2].innerHTML;
            f4.value = cells[3].innerHTML;
            f5.value = cells[4].innerHTML;
            f6.value = cells[5].innerHTML;
        };
    }
}

JSFIDDLE: code / result page

Community
  • 1
  • 1
Stano
  • 7,265
  • 6
  • 26
  • 41
3

With using just plain javascript you can do this:

<html>

<head>

<script language="javascript" type="text/javascript">
function showRow(row)
{
var x=row.cells;
document.getElementById("firstname").value = x[0].innerHTML;
document.getElementById("lastname").value = x[1].innerHTML;
document.getElementById("age").value = x[2].innerHTML;
document.getElementById("total").value = x[3].innerHTML;
document.getElementById("discount").value = x[4].innerHTML;
document.getElementById("diff").value = x[5].innerHTML;
}
</script>
</head>
<body>
<table id="myTable" cellspacing="1">             
    <thead>
        <tr> 
            <th>first name</th> 
            <th>last name</th> 
            <th>age</th> 
            <th>total</th> 
            <th>discount</th> 
            <th>diff</th> 
        </tr> 
    </thead> 
    <tbody> 
        <tr onclick="javascript:showRow(this);"> 
            <td>peter</td> 
            <td>parker</td> 
            <td>28</td> 
            <td>9.99</td> 
            <td>20.3%</td> 
            <td>+3</td> 
        </tr> 
         <tr onclick="javascript:showRow(this);"> 
            <td>john</td> 
            <td>hood</td> 
            <td>33</td> 
            <td>19.99</td> 
            <td>25.1%</td> 
            <td>-7</td> 
        </tr> 
         <tr onclick="javascript:showRow(this);"> 
            <td>clark</td> 
            <td>kent</td> 
            <td>18</td> 
            <td>15.89</td> 
            <td>44.2%</td> 
            <td>-15</td> 
        </tr> 
         <tr onclick="javascript:showRow(this);"> 
            <td>bruce</td> 
            <td>almighty</td> 
            <td>45</td> 
            <td>153.19</td> 
            <td>44%</td> 
            <td>+19</td> 
        </tr> 
        <tr onclick="javascript:showRow(this);"> 
            <td>bruce</td> 
            <td>evans</td> 
            <td>56</td> 
            <td>153.19</td> 
            <td>23%</td> 
            <td>+9</td> 
        </tr> 
    </tbody> 
</table>

Firstname is:<input type="text" id="firstname" />
<br>
Lastname is:<input type="text" id="lastname" />
<br>
Age is:<input type="text" id="age" />
<br>
Total is:<input type="text" id="total" />
<br>
Discount is:<input type="text" id="discount" />
<br>
Diff is:<input type="text" id="diff" />


</body>
</html>
Avitus
  • 14,677
  • 6
  • 41
  • 53
2

Try this (see the fiddle):

var table = document.getElementById( 'myTable' ),
    inputHash = {
        '0': 'firstname',
        '1': 'lastname',
        '2': 'age',
        '3': 'total',
        '4': 'discount',
        '5': 'diff'
    };

for ( var i in inputHash )
    inputHash[ i ] = document.getElementById( inputHash[ i ] );

table.addEventListener( 'click', function( evt ) {
    var target = evt.target;

    if ( target.nodeName != 'TD' )
        return;

    var columns = target.parentNode.getElementsByTagName( 'td' );

    for ( var i = columns.length; i-- ; )
        inputHash[ i ].value = columns[ i ].innerHTML;
} );
oleq
  • 17,023
  • 2
  • 34
  • 63
  • This will not work if you're using an older browser because older browsers do not support "addEventListener". This is why in jQuery they do something else. Please read: http://stackoverflow.com/questions/4810766/how-does-jquerys-click-work-behind-the-scenes – Avitus Jul 31 '13 at 20:29
  • why clicked element is not loaded to textbox http://jsfiddle.net/rrzZU/703/ – srinivas gowda Jan 28 '17 at 07:29
0

Place an onclick on each of your trs like this:

onclick="fillme(this)"

and then place this script in your head

<script>
function fillme(o){
    var inputs=document.getElementsByTagName('input');
    var tds=o.getElementsByTagName('td');

    for(a in tds){
        inputs[a].value=tds[a].innerHTML;
    }
}
</script>
Adam MacDonald
  • 1,868
  • 15
  • 19
  • This will only work if the inputs are in the same order as the TD. If they are in a different order your data will display wrong. – Avitus Jul 31 '13 at 20:29
  • Hello Adam, try to update `for(a in cells)` with: `for(var a in cells){ if (!cells.hasOwnProperty(a)) { continue; } inputs[a].value=tds[a].innerHTML; }` to assign only number indexes. Other than that, the solution is ok. – Stano Jul 31 '13 at 21:07