JavaScript, HMLT: changing color of selected row in table

2021. 8. 14. 23:28Frontend/Web programming

    목차
반응형

<table id="time_table" 
...
<tr onclick="javascript:onRowSelected(this);">
<td> ... </td>
<script type="text/javascript">
    function onRowSelected(target) {
        var tbody = target.parentNode;
        var trs = tbody.getElementsByTagName('tr');

        var bg_color = "#ffffff";
        var text_color = "black";
        var sel_bg_color = "#82B27D";
        var sel_text_color = "#ffffff";

        var upm_id = "";
        var upm_name = "";

        for (var i = 0; i < trs.length; i++) {
            if (trs[i] != target) {
                trs[i].style.backgroundColor = bg_color;
                trs[i].style.color = text_color;
            } else {
                trs[i].style.backgroundColor = sel_bg_color;
                trs[i].style.color = sel_text_color;
                var td = trs[i].getElementsByTagName('td');
                upm_id = td[0].innerText;
                upm_name = td[1].innerText;
            }
        }
...
반응형