Visited in table tr

0

I have a table that when a tr is clicked, a screen is generated in the page itself according to what was selected, I need a css code (if possible), that when the tr is clicked it will have a different background of the others, when another tr is clicked, it has a different background like the: active of tag a. In my table each tr has a different id and name.

Table:

        <tr id='ana' name='ana'>
            <td>1</td>
            <td>Ana</td>
            <td>Informática</td>
        </tr> 
         <tr id='bia' name='bia'>
            <td>2</td>
            <td>Bia</td>
            <td>Logística</td>
        </tr>
         <tr id='carlos' name='carlos'>
            <td>3</td>
            <td>Carlos</td>
            <td>Redes</td>
        </tr>
    
asked by anonymous 26.07.2017 / 21:15

3 answers

1

Would that be what you want to do?

$("tr").click(function() {
  $('tr').not(this).css({"background-color": "white", "color": "black"});
  $(this).css({"background-color": "#6495ED", "color": "red"});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr id='ana' name='ana'>
  <td>1</td>
  <td>Ana</td>
  <td>Informática</td>
</tr> 
<tr id='bia' name='bia'>
  <td>2</td>
  <td>Bia</td>
  <td>Logística</td>
</tr>
<tr id='carlos' name='carlos'>
  <td>3</td>
  <td>Carlos</td>
  <td>Redes</td>
</tr>
</table>

What the above code does: By clicking on a tr of the table it places all tr with background-color white and text color black except this ( tr clicked) and then apply the% blue_and% of text to red in the% color of% clicked.

    
26.07.2017 / 21:44
0

Unfortunately there is no way to do this with only css, because the TR tag is not a naturally selectable element. However, you can use Javascript to add a specific class in the last selected TR, and manipulate your style through it. Here's an example:

CSS:

tr {
    background-color: transparent;
}

tr.selected {
    background-color: #DDD;
}

Javascript:

var el = document.getElementsByTagName("tr");
for(i = 0; i < el.length; i++){
    el[i].onclick = function(){
        var trSelected = document.getElementsByClassName("selected");
        if(trSelected.length > 0)
            trSelected[0].classList.remove("selected");
        this.classList.add("selected");
    }
}

Or, if you use jQuery in your application:

$("tr").click(function(){
    $("tr.selected").removeClass("selected");
    $(this).addClass("selected");
});

NOTE: Remembering that style manipulation, for organizational reasons, should always be done from a class, not directly in js.

    
26.07.2017 / 22:04
0

Every time you click on the tr element the function will add the clique class that exists in the CSS, that is, blue background and white letters

$(function(){
  $('tr').click(function(){
    $(this).addClass('clique');
    $('tr').not(this).removeClass('clique');
  })
});
.clique {
background: #03c;
//para que as letras não fiquem obscuras
color:white;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr id='ana' name='ana'>
  <td>1</td>
  <td>Ana</td>
  <td>Informática</td>
</tr> 
<tr id='bia' name='bia'>
  <td>2</td>
  <td>Bia</td>
  <td>Logística</td>
</tr>
<tr id='carlos' name='carlos'>
  <td>3</td>
  <td>Carlos</td>
  <td>Redes</td>
</tr>
</table>
    
26.07.2017 / 22:10