-2

I am trying to color tr with .click event using jquery.

My code has no problem with coloring the first row which is static, but other rows, created with jQuery don't want to color.

I have no idea why is not working on other rows.

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

Here is my existing code: `http://jsfiddle.net/1mihcc/7okvbkg2/1/

jmore009
  • 12,863
  • 1
  • 19
  • 34
mihcc
  • 45
  • 1
  • 9

1 Answers1

0

That's because the elements are not in the DOM when you run the jQuery selector. Use event delegation.

$(function () {
    $(document).on('click', 'tr', function () {
        $(this).toggleClass("obarva");
    });
});
Alexander O'Mara
  • 58,688
  • 18
  • 163
  • 171
  • Yes i did that, and it works perfect in jsFiddle, but when I run same code from netbeans, coloring doesn't work. I have css in html and js as other file. – mihcc Jan 12 '15 at 07:11