Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select just a row of table using jQuery

I have created a table on my application same following code

<table class="spreadsheet">
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
<td>6</td>
</tr>
</table>

I select a row by click with following jQuery code

 $("tr").click(function(){
 $(this).toggleClass("otherstyle");
}

and my css is

tr.otherstyle td
{
    background-color: #d1db3e;
    color:#000;
}

I would like when I click on a row, other rows be unselected, and just one row be selected.

How we could create this?

like image 483
user1400 Avatar asked Dec 08 '22 03:12

user1400


1 Answers

$("table.spreadsheet tr").click(function(){
    $("table.spreadsheet tr").removeClass("otherstyle");
    $(this).toggleClass("otherstyle");
});

See a working demo

like image 139
rahul Avatar answered Dec 27 '22 14:12

rahul