Reputation: 399
I have just started learning jQuery today, i have written up a code in a javascript file. This code is designed to make a button fade when hovered over it, and then return to normal after moving the mouse away. Now as i have just said, i am very new to jQuery, and that means i am thinking this is down to setting it up wrong. Here is my javascript contents :
$(document).ready(function(){
$(".buttons").mouseenter(function(){
$(".buttons").fadeTo("fast",0.25);
});
$(".buttons").mouseleave(function(){
$(".buttons").fadeTo("slow",1);
});
});
This javascript file is saying to fade my class "buttons" when i hover over them.. I have linked my HTML file to this js file with:
<script type="text/javascript" src="script.js">
I know that links up to the javascript file correctly, as i open my html and the console says "$ is not defined" on line 1. Now that is the very first line of my javascript. So clearly my html is opening my JS file, but isnt liking the $ on the very first line.
Again, ill repeat, i am very new to this and anything that should be obvious, will not be obvious to me. Thanks for any help i get.
Upvotes: 0
Views: 1091
Reputation: 6404
The dollar sign belongs to jQuery libraries namespace so you need to include jQuery before using it. After including the library you can use these functions, e.g. for selection via $('.classname')
.
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
This includes the latest jQuery release in the minified version. You can load and host this file local as well.
Upvotes: 4
Reputation: 6937
Did you include jQuery in your html file? Something like
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
This must be included before the first use of $, thus before including your script.js
Upvotes: 2