Click here to Skip to main content
16,021,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,


Why is this working just fine:
jQuery(document).bind('keydown', function (evt) {
        if (evt.keyCode == 38) {
            var element = document.getElementById("table_row_2");
            element.style.backgroundColor = "red";
        }
    });

But this isn't working:
jQuery(document).bind('keydown', function (evt) {
        if (evt.keyCode == 38) {
            var element = document.getElementById("table_row_2");
            element.css("background-color","red");
        }
    });


What I have tried:

while this again works just fine:
JavaScript
<pre lang="text">$(document).ready(function () {
        $("tr").hover(function () {
            $(this).css("background-color", "red");
        }, function () {
            $(this).css("background-color", "red");
        });
    });


Im confused...
Posted
Updated 26-Feb-17 23:42pm

1 solution

css()[^] is a jQuery method that works on jQuery selectors, so
instead of :
element.css("background-color","red");

You have to turn it into a jQuery selector:
$(element).css("background-color","red");

In your last example, the $(this) refers to the currently selected object in an event which in your case is the "tr" that is being hovered over.
Learn more about jQuery Selectors[^]
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900