Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
14
rated 0 times [  17] [ 3]  / answers: 1 / hits: 20657  / 10 Years ago, fri, august 15, 2014, 12:00:00

I'm trying to make it so that when a row is clicked, it gets highlighted (to make it clear that it is the currently selected row). And when another row is clicked, the previously selected row becomes un-highlighted and the new one becomes highlighted.



As of now, what happens is that when I hover over a row, it gets highlighted (which works as planned), but when I click the background-color of the rows do not change. Some of my code is below, thanks in advance.



Here is some example HTML:



    <tr class=text_data selected_grey onclick=getReportDetails(this, '[email protected]');>
<td class=text>John</td>
<td class=text> Doe</td>
</tr>
<tr class=text_data selected_grey onclick=getReportDetails(this, '[email protected]);>
<td class=text>Sarah</td>
<td class=text>Dean</td>
</tr>


This is the relevant part of my Javascript getReportDetails function.



function getReportDetails(elem, email) {


var j_elem = $(elem);

$(.text_data).each(function() {
if ($(this).is(j_elem)) {
j_elem.addClass(selected_grey);
} else {
$(this).removeClass(selected_grey);
}
});


And this is the involved CSS.



.text_data {
font-family: Verdana;
font-size: 12px;
font-weight: none;
}
.text_data:hover{
cursor: pointer;
background-color: #E0E0E0;
}
.selected_grey {
background-color: #E0E0E0;
}


EDIT: It turned out to just be a cache problem.


More From » jquery

 Answers
13

To toggle the table row click, check like below:



JSFiddle: http://jsfiddle.net/ta6r6e7g/



HTML:



<table>
<tr class=text_data selected_grey onclick=getReportDetails(this, '[email protected]');>
<td class=text>John</td>
<td class=text> Doe</td>
</tr>
<tr class=text_data selected_grey onclick=getReportDetails(this, '[email protected]);>
<td class=text>Sarah</td>
<td class=text>Dean</td>
</tr>
</table>


JQuery:



$(.text_data td).on(click, function() {
var tr = $(this).parent();
if(tr.hasClass(selected)) {
tr.removeClass(selected);
} else {
tr.addClass(selected);
}

});


CSS:



tr.selected td {
background-color: #333;
color: #fff;
}

[#69770] Wednesday, August 13, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
irvingcarloe

Total Points: 677
Total Questions: 109
Total Answers: 96

Location: Svalbard and Jan Mayen
Member since Sun, Sep 25, 2022
2 Years ago
irvingcarloe questions
Wed, Mar 31, 21, 00:00, 3 Years ago
Tue, Aug 4, 20, 00:00, 4 Years ago
Fri, Jul 3, 20, 00:00, 4 Years ago
;