ariel
ariel

Reputation: 3072

How do I change the color of an element via jQuery?

This is driving me crazy. I think I have everything right but I am unable to change the color via jQuery. I first was doing this locally then I thought that jQuery can't be used locally, which I learned was incorrect but to make it work I uploaded to my server and it still doesn't work.

What am I doing wrong?

<!doctype html>
<html>
<head>
     <title>Test</title>
     <script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
     <script>
          $("p").css('color', 'blue');
     </script>
     <p>Hello</p>
</body>
</html>

Upvotes: 0

Views: 45

Answers (6)

Dasari Vinodh
Dasari Vinodh

Reputation: 103

The above solutions are perfect here is the reason why ? ?

in java script every function is called when event occurs in jquery the first event is on load event when the the code is ready to execute ready event is fired all the functions have to be called with in these ready event only

Upvotes: 0

Prasanth K C
Prasanth K C

Reputation: 7332

Wrap it in document.ready function, Like this,

$(document).ready(function() {
    $("p").css('color', 'blue');
});

Upvotes: 0

Manoj
Manoj

Reputation: 1890

Try this code .Refer

JS:

$(document).ready(function() {
    $("p").css('color', 'blue');
});

Upvotes: 0

underscore
underscore

Reputation: 6877

WORKING DEMO

.ready()

Description: Specify a function to execute when the DOM is fully loaded.

$(document).ready(function() {
    $("p").css('color', 'blue');
});

Upvotes: 0

Dhaval Marthak
Dhaval Marthak

Reputation: 17366

You only forgot to wrap your code inside DOM ready handler

$(document).ready(function(){
   $("p").css('color', 'blue');
})

See it Working here

Upvotes: 1

Felix
Felix

Reputation: 38102

Put your code inside

$(document).ready(function() {
    $("p").css('color', 'blue');
});

Upvotes: 0

Related Questions