0

I'm trying to include jQuery in my HTML-file but I haven't succeed yet.. I know there are a lot of tutorials and stuff on the internet (and this site!) but it still won't work. This is my code in the HTML-file.

<!DOCTYPE html>
<html>
<head>  
<link type="text/css" rel="stylesheet" href="global.css"/>  
<script type="text/javascript" src="jquery-2.0.3.min.js"></script>      
<title>test</title>
</head>
<body>
  <script>
    $(document).ready(function() {
        $("p").css("font-size:50px");
    };
  </script>
<p id="first">Hello there!</p>
</body>
</html>

I just don't understand why nothing is happening.. Would be awesome if someone could help me with this on!

6
  • 4
    did you place the jquery-2.0.3.min.js file in the same directory as the source file Commented Dec 30, 2013 at 16:36
  • else use the cdn version like <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.js"></script> Commented Dec 30, 2013 at 16:37
  • 2
    also to set the css value you need to use $("p").css("font-size", "50px"); Commented Dec 30, 2013 at 16:37
  • 3
    You also are missing a ); closing your DOM ready, which throws syntax error, which bricks your script. Potential cause. Commented Dec 30, 2013 at 16:38
  • tymeJV beat me to it.. Commented Dec 30, 2013 at 16:39

5 Answers 5

2

You are not using the jQuery css() function quite properly. You also missed a closing parenthesis. Try changing it to this:

<script>
$(document).ready(function() {
    $("p").css("font-size", "50px");
});
</script>

You should always use .css('property-name', 'property-value') in that format for best results. There are other ways (object properties) but they are slightly different syntax and do not conform directly to expected vanilla CSS property names.

jQuery API reference - .css()

If that does not fix it, check to be sure your jquery file is in the same directory as the HTML page. Or (probably better) just replace it with the CDN call recommended by other answers (e.g. replace <script type="text/javascript" src="jquery-2.0.3.min.js"></script> in your head with <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.js"></script>).

Sign up to request clarification or add additional context in comments.

Comments

1

You missed to close the code properly. You missed the );. Also the css method is little different in your version. The parameters should be property name, property value.

Try this

$(document).ready(function() {
   $("p").css("font-size","50px");
});

Working sample : http://jsbin.com/AfAjihiP/1/

Comments

1

There is a small syntax error. Replace:

$(document).ready(function() {
        $("p").css("font-size:50px");
    };

With this:

$(document).ready(function() {
        $("p").css("font-size", "50px");
    });

You forgot to close a parenthesis, and the .css() function needs two parameters.

1 Comment

@putvande: I'm sorry, I forgot that the .css() function needs two paraters. I updated my answer.
0

YOu can do one of two things. You can use the cdn version and link it like this:

or

<script type="text/javascript" src="jquery-2.0.3.min.js"></script> after making sure the file is in the root of your project. also you should have:

<script>
$(document).ready(function() {
    $("p").css("font-size", "50px");
});
</script>

and not what you currently have.

Comments

0

Don't write several code: Instead of $(document).ready use $ , it works:

so :

$(function() {
        $("p").css({"font-size": "50px"});
  });

or

$(function() {
        $("p").css("font-size", "50px");
});

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.