0

I've a list of data from db which I'm showing in each row on a Table. I want when I click a row it's should be remain highlighted with color. But my following code is highlighting all row. Can you help me about it.

It's should be highlight only one row which I clicked.

My code

<script type="text/javascript">
function visited(a) {
tr = a.parentNode.parentNode;
tr.style.backgroundColor = "red";
}
</script>

<?php
    echo "<table width='100%' cellpadding='0' cellspacing='0'>";
    echo "<thead>";
    echo "<tr>";                
    echo "<td class='' valign='top' width='200'></td>";
    echo "<td class='' valign='top' width='125'></td>";                             
    echo "<td class='' valign='top' width='125'></td>";                             
    echo "<td class='' valign='top' width='125'></td>";                             
    echo "<td class='' valign='top' width='125'></td>";                             
    echo "</tr>";   
    echo "</thead>";
    echo "<tbody>";             

while($res =  mysql_fetch_array($get)){
    $cdid = $res['cdid'];
    $family_name = $res['family_name'];
    $given_name = $res['given_name'];
    $work_phone = $res['work_phone'];
    $mobile_phone = $res['mobile_phone'];
    $email = $res['email'];
    $email_private = $res['email_private'];
    $cid = $res['cid'];
    $department = $res['department'];
    $title = $res['title'];

    $getComapnyName =  mysql_query("SELECT company_name FROM company WHERE cid = '$cid' ");
    $resCompany =  mysql_fetch_array($getComapnyName);
    $companyName =  $resCompany['company_name'];

    if (strlen($companyName) >= 20) {
        $companyName =  substr($companyName, 0, 10). " ... " . substr($companyName, -5);
    }else{
        $companyName = $companyName;
    }           
    echo "<tr onclick='getDetails($cdid),showNotexBox($cdid),showAllNotesBox($cdid), visited(this);'>";                             
    echo "<td class='' valign='top'>$companyName</td>";
    echo "<td class='' valign='top'>$family_name</td>";
    echo "<td class='' valign='top'>$given_name</td>";
    echo "<td class='' valign='top'>$department</td>";
    echo "<td class='' valign='top'>$title</td>";

    echo "</td>";
    echo "</tr>";
    }
    echo "</tbody>";
    echo "</table>";    
?>

2 Answers 2

1

If you want to go with jQuery you can do the following. Just copy paste the code and run it

  <html>
     <head>
      <meta charset="UTF-8">
      <title></title>
      <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">  </script>

      <script type="text/javascript">


    var Color = {
        initialize: function() {
                $(document).on("click", ".trow", Color.setColor)                                                
        },
        setColor: function() {

            $(".trow").css('background-color','#fff');
            this.style.backgroundColor = 'red';            
        }
    };          
    $(document).ready(function(){
        Color.initialize();
    });     
    </script>        
   </head>
   <body>
   <table width='100%' cellpadding='0' cellspacing='0'>
   <tbody>                
   <tr class='trow' id='r1'>                             
    <td class='' valign='top'>value1</td>
    <td class='' valign='top'>value2</td>
    <td class='' valign='top'>value3</td>
    <td class='' valign='top'>value4</td>
    <td class='' valign='top'>value5</td>
   </tr>
   <tr class='trow' id='r2'>                             
    <td class='' valign='top'>price1</td>
    <td class='' valign='top'>price2</td>
    <td class='' valign='top'>price3</td>
    <td class='' valign='top'>prive4</td>
    <td class='' valign='top'>price5</td>
   </tr>    
   </tbody>
   </table>
   </body>
   </html>

Anyway, you can add or remove classes with jQuery and use css for styling

Sign up to request clarification or add additional context in comments.

Comments

0

Since your a argument is referencing the <tr> being clicked, by using a.parentNode.parentNode you're actually changing the backgroundColor of the table.

Try this instead:

function visited(tr) {
    tr.style.backgroundColor = "red";
}

As per your edit, try this:

function visited(tr) {
    var table = tr.parentNode.parentNode;
    var trs = table.getElementsByTagName('tr');

    for (var i = 0; i < trs.length; i++)
        {
        trs[i].style.backgroundColor = null;
        trs[i].style.color = null;
        }

    tr.style.backgroundColor = '#000';
    tr.style.color = '#fff';
}

10 Comments

Now it's highlighting. But it's remain highlighted which row's I clicked. It's should be highlight only one row which I clicked.
WoW. It's working. But text color is black. so I'm using tr.style.Color = "white"; but not working.
Try lowercased color.
I used it color but now full row is white which row I clicked !
You really should use CSS for this. Anyway, I revised my answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.