Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have
a html table containing Rows as Below
<html>
<head></head>
<body>
<table>
<tr><td><a href="abcd.aspx">ABCD</></td>
</tr>
<tr><td><a href="xyz.aspx">XYZ</></td>
</tr>
</table>
</body>
</html>

this Table set on the Masterpage
Now I want that on click of link ABCD or XYZ
whole row must get selected and also the
colour of that selected rows must change
accordingly.
can this be done.How can i do this
please assist.
Posted

XML
<table>
  <tr>
    <td onclick="changeRowColor(this)">ABC
    </td>
  </tr>
  <tr>
    <td onclick="changeRowColor(this)">DEF
    </td>
  </tr>
  <tr>
    <td onclick="changeRowColor(this)">...
    </td>
  </tr>
  <tr>
    <td onclick="changeRowColor(this)">XYZ
    </td>
  </tr>
</table>



XML
<script type="text/javascript">
    function changeRowColor(currentRow) {
        $(currentRow).css('color', 'green'); // change clicked row to active color
        $(currentRow).parent().siblings().css('color', 'red'); // change all the rest to non active color
    }
</script>




Please refer JQuery Library in you page, and let me know if you have any query on the same

Thanks,
Ambesha
 
Share this answer
 
Comments
AshishChaudha 15-Oct-12 6:40am    
my +5 Ambesha
Karwa_Vivek 16-Oct-12 6:43am    
have some problem that
after clicking on other link the visited link is not resseting its colour.
wheres the problem
Karwa_Vivek 16-Oct-12 6:44am    
Pasting my code here please help


<html>
<head>
<title></title>
<script type="text/javascript">
function changeRowColor(currentRow) {
$(currentRow).css('color', 'green'); // change clicked row to active color
$(currentRow).parent().siblings().css('color', 'red'); // change all the rest to non active color
}
</script>
<script type="text/javascript" src="Scripts/jquery.min.js"></script>
</head>
<body>
<table>
<tr>
<td önclick="changeRowColor(this)">ABC
</td>
</tr>
<tr>
<td önclick="changeRowColor(this)">DEF
</td>
</tr>
<tr>
<td önclick="changeRowColor(this)">...
</td>
</tr>
<tr>
<td önclick="changeRowColor(this)">XYZ
</td>
</tr>
</table>
</body>
</html>
I have tried tried the solution using JQuery/Javascript


jQuery

JavaScript
$('td').click(function() {
   $(this).css('backgroundColor', '#000');
});

...or....

$('table').on('click', 'td', function() {
   $(this).css('backgroundColor', '#000');
});

JavaScript
JavaScript
var td = document.getElementsByTagName('td');

var changeStyle = function (element) {
    element.style.backgroundColor = '#000';
}

for (var i = 0, length = td.length; i < length; i++) {

    if (td[i].addEventListener) {
        td[i].addEventListener("click", function() {
           changeStyle(td[i]);
        }, false); 
    } else if (td[i].attachEvent) {
        td[i].attachEvent("onclick", function() {
           changeStyle(td[i]);
        });
    } else {
        td[i].onclick = function() {
           changeStyle(td[i]);
        };
    }
}

...or...
JavaScript
var table = document.getElementsByTagName('table')[0];

var changeStyle = function(e) {
    e = e || window.event;
    var element = e.target || e.srcElement;
    element.style.backgroundColor = '#000';
}

if (table.addEventListener) {
    table.addEventListener("click", changeStyle, false);
} else if (table.attachEvent) {
    table.attachEvent("onclick", changeStyle);
} else {
    table.onclick = changeStyle;
}​


jQuery
JavaScript
$('td').click(function() {
   $(this).addClass('active');
 );

...or....

$('table').on('click', 'td', function() {
   $(this).addClass('active');
});

CSS
CSS
td.active {
  background: #000;
}

The reason this didn't work...

<table><tbody><tr><td style="background-color:white">
     onclick="$(this).onmouseover('background-color','black')">
     SomeText
</td></tr></tbody></table>
 
Share this answer
 
put



function changeRowColor(obj)
{
$(obj).addClass('active');
}


hope this help
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900