2

I have read almost every article about this... Just can't find the reason why this isn't working. I want to change my tr background-color using jQuery. There are no errors in web inspector or what so ever, just nothing happens...

My html code:

<!DOCTYPE html>
<html>
  <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
    <title>Opravila</title>
    <style>
      .slika 
      {
      width:20px;
      height:20px;
      }
      tr
      {
      background-color:white;
      }

      tr.highlight
      {
      background-color:yellow;
      }

    </style>
  </head>
  <body>
    <script src="script.js" type="text/javascript"></script>
    <form>
      Naslov opravila: <input type="text" id="naslov"></input>
      Vrsta opravila: <input type="text" id="vrsta"></input>
      Nujnost opravila: 
      <select id="nujnost">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
      </select>
      <input type="button" value="Dodaj opravilo" id="dodaj"></input>

    </form>

    <img src="minus.png" class="slika" id="slika"></img>
    <table id="tabela">
      <thead>
        <tr>
          <th>#</th>
          <th>Opravilo</th>
          <th>Vrsta</th>
          <th>Nujnost</th>
          <th>Datum vnosa</th>
        </tr>
      </thead class="vrstica">
      <tbody>
      </tbody>
    </table>

  </body>
</html>

And my jQuery code: (Note that my code for this starts almost at the end)

$(document).ready(function(){
    $("#dodaj").click(function() {
        var naslov=$("#naslov");
        naslov=naslov.val();
        var vrsta=$("#vrsta");
        vrsta=vrsta.val();
        var nujnost=$("#nujnost");
        nujnost=nujnost.val();
        var zaporedna_st=$("#tabela tbody tr").length;

        var datum =new Date();
        datum=datum.getDate()+"."+(datum.getMonth()+1)+"."+datum.getFullYear();

        $("#tabela tbody").append("<tr><td>"+(zaporedna_st+1)+"</td><td>"+naslov+"</td><td>"+vrsta+"</td><td>"+nujnost+"</td><td>"+datum+"</td></tr>");
    });

    $("#tabela tbody tr").click(function() {
        $(this).css("backgroundColor", "red");
    });
});
Barmar
  • 741,623
  • 53
  • 500
  • 612
spafrou
  • 548
  • 1
  • 6
  • 19

6 Answers6

1

You need to do like this

$("#tabela tbody").on('click', 'tr', function() {
    $(this).css("backgroundColor", "red");
});
adeneo
  • 312,895
  • 29
  • 395
  • 388
0

You need to do like this

$("#tabela tbody").on('click', 'tr',function() {
    $(this).css("backgroundColor", "red");
});

you need to use on because the tr elements are dynamically created. Otherwise the click event will not be bind to the newly created tr elements.

Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
0

Try this,

$("#tabela tbody").click(function() {
     $(this).find('tr').css('backgroundColor', 'red');
});
Krish R
  • 22,583
  • 7
  • 50
  • 59
0

First of all you can't set background to tr tag, only for td

Another point is set color like this

$('td').css({'backround-color: 'blue'})

OK sorry for my mistake.

Really background property works well for tr

Here I prepare your example with correct jQuery selector

http://jsfiddle.net/XJb98/

Just click to table header (because in example I don't have tbody I change it to thead)

Bobbe
  • 53
  • 1
  • 1
  • 6
0

Please try this..

$("#tabela :tr").click(function(e) {
    var currentRowId = this.id;
    $("#"+currentRowId ).css("backgroundColor", "red");
});
Arpit Jain
  • 455
  • 3
  • 8
-1

If you want to change TR background to red color on TR click then use the following code

//on ready state
$(function(){
  //Change TR background color to red on TR click Event
  $("#tabela").find("tr").on('click', function() {
    $(this).css({"background":"red"});
  });
});

If you want to change TR background to red color on document ready then use the following code

//on ready state

$(function(){
  //Change all TR background color to red
  $("#tabela").find("tr").css({"background":"red"});
});
Mohan Singh
  • 883
  • 8
  • 18