Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I put jquery in a separate file?

I've been googling for the past hour or so but can't seem to figure out a solution to this problem. I just finished jQuery course from codecademy, and am now working on a project. For some reason my code jquery code will not work

jQuery:

$(document).ready(function(){
$("div").css("border", "3px solid red");
$(".storyblocks").mouseenter(function(){
    $(this).animate({
        height: "+= 20px"
        width: "+= 20px"
    });
});

$(".storyblocks").mouseleave(function(){
    $(this).animate({
        height: "-= 20px"
        width: "-= 20px"
    });
});
}); 

HTML:

<!DOCTYPE html>
<html>
<head>
<title>Project Website</title>
<link type = "text/css" rel = "stylesheet" href = "stylesheet.css"/>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript" src="script.js"></script>

</head>

I added the $("div").css("border", "3px solid red"); to check if divs have red border, and so far no luck. Some say that I don't need $(document).ready, but I don't understand why not.

Please help?

like image 574
user2967568 Avatar asked Oct 19 '25 02:10

user2967568


1 Answers

Your issue with your jQuery not working doesn't have to do with having your jQuery in a separate file, it has to do with a small syntax error: you don't have a comma in between your animate properties, and your animate properties are using incorrect syntax.

Here's your code, but with the necessary commas, and proper syntax for animating properties:

http://jsfiddle.net/4xfAZ/2/

The comma is here:

$(document).ready(function () {

    $("div").css("border", "3px solid red");
    $(".storyblocks").mouseenter(function () {
        $(this).animate({
            height: ($(this).height() + 20) + 'px',
            width: ($(this).width() + 20) + 'px'
        });
    });

    $(".storyblocks").mouseleave(function () {
        $(this).animate({
            height: ($(this).height() - 20) + 'px',
            width: ($(this).width() - 20) + 'px'
        });
    });
});

After the height declaration, before the next declaration, you need to separate by commas.

Because your jQuery errored, it didn't do anything, which is why you didn't see the red border :) even though the red border code is separate to the offending code.

HTH

like image 174
Ming Avatar answered Oct 21 '25 14:10

Ming