I want my script to highlight the row that I select and it works great, but when a row is selected/highlighted i want it to be "deselected/dehighlighted" if another row is selected. How do i do this?
Here is current code for selecting a row, it deselects, but only if i click on the same row again:
$(".candidateNameTD").click(function() {
$(this).parents("tr").toggleClass("diffColor", this.clicked);
});
Html table
<table id="newCandidatesTable">
<thead>
<tr>
<th style="cursor: pointer;">ID</th>
<th style="cursor: pointer;">Navn</th>
<th style="cursor: pointer;">Email</th>
<th></th>
</tr>
</thead>
<tbody>
<% foreach (var candidate in Model.Ansogninger)
{
%>
<tr id="<%= candidate.AnsogerID %>" class="newCandidatesTableTr">
<td><div id="candidateID"><%= candidate.AnsogerID %></div></td>
<td><div id="<%= "candidateName_" + candidate.AnsogerID %>" class="candidateNameTD"><%= candidate.Navn %></div></td>
<td><div id="candidateEmail"><%= candidate.Email %></div></td>
<td>
<div id="<%= "acceptCandidateButton_" + candidate.AnsogerID %>" class="acceptb" style="cursor: pointer; border: 1px solid black; width: 150px; text-align: center;">Godkend</div>
</td>
</tr>
<%
} %>
</tbody>
</table>
Change the 'background-color' property of the row in the click event handler of the row. Code would be: //attach an onclick event handler to the table rows $(“#myTable tbody tr”). click(function(){ //change the 'background-color' property to the desired color.
You could first deselect all rows, like
$(".candidateNameTD").click(function() {
$(this).closest("tr").siblings().removeClass("diffColor");
$(this).parents("tr").toggleClass("diffColor", this.clicked);
});
edit: yep, sry, but the idea was right ;)
$(".candidateNameTD").click(function() {
$("tr").removeClass("diffColor");
$(this).parents("tr").addClass("diffColor");
});
This will only affect the current table:
$(".candidateNameTD").click(function() {
$('table#newCandidatesTable tr').removeClass("diffColor");
$(this).parents("tr").addClass("diffColor");
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With