Mark Alexa
Mark Alexa

Reputation: 125

Changing css property via jQuery

What I'm trying to achieve here is when I hover over the link it would turn green.

What is exactly wrong with this code:

<script>
     $(document).ready(function() {
     $("a").hover(function() {
     $(this).css({"background-color": "green;"});
     });
 });

</script>

Upvotes: 1

Views: 45

Answers (2)

Ashwin Parmar
Ashwin Parmar

Reputation: 3045

jQuery having .css() function which will allow you to change CSS property or any DOM Element on document.

Single Property Example:

jQuery(Selector).css("PropertyName", "Value");

Multiple Property Example:

jQuery(Selector).css({"PropertyName1": "Value1", "PropertyName2": "Value2"});

e.g.

jQuery(document).ready(function() {
    jQuery("a").hover(function() {
        jQuery(this).css("background-color", "green");
    });
});

Upvotes: 0

adeneo
adeneo

Reputation: 318352

It's the semicolon after green;, that works in CSS, but not in javascript, which expects a color only, no semicolon.

$(document).ready(function() {
     $("a").hover(function() {
         $(this).css({"background-color": "green"});
     });
});

Upvotes: 1

Related Questions