0

On hover I want to change background from a div. therefore I used Jquery. But somehow it doesn't work, not in an empty file, not in an HTML file and not in a file with different Jquery codes.

$('#b').hover(function() {
  $('#a').css({
    'background': '#ccc'
  });
}, function() {
  $('#a').css({
    'background': 'initial'
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="a">Div A</div>
<div id="b">Div B</div>

1
  • 5
    Your code works fine as you can see from the snippet I edited in to your question. Check the console for errors. It's likely to be a configuration error, such as not including jquery.js, incorrect path, or missing document.ready handler. Commented Jan 29, 2018 at 12:33

1 Answer 1

2
Your code in working condition please place your jquery code after jquery library  
`<!DOCTYPE html>
    <head>

    </head>
    <body>
       <div id="a">Div A</div>
       <div id="b">Div B</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

        <script>
$('#b').hover(function() {
  $('#a').css({
    'background': '#ccc'
  });
}, function() {
  $('#a').css({
    'background': 'initial'
  });
});
</script>
    </body>
</html>`
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.