1

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>
3
  • You added a semi-colon after green. This is incorrect css value Commented Nov 5, 2016 at 16:28
  • do you know that on mouseleave the anchor will still be green? Just asking. Otherwise such things should be handled by CSS, not JS Commented Nov 5, 2016 at 16:34
  • Dammit ! XD One stupid semicolon. It works now. Thanks a lot ! Commented Nov 5, 2016 at 16:39

2 Answers 2

1

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"});
     });
});
Sign up to request clarification or add additional context in comments.

Comments

0

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");
    });
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.