I want to Change the background colour on click . This is my code work that i tried.pls help me out :)
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
$(document).ready(function(){
$(#co).click(change()
{
$(body).css("background-color":"blue");
});
});
Css code
body
{
background-color:red;
}
Body code
<body>
<div id="co" click="change()">
hello
</div>
To set the background color using jQuery, use the jQuery css() property. We will set background color on mouse hover with the jQuery on() method.
To change the background color using jQuery, use the jQuery css() property. We will change background color on mouse hover with the jQuery on() and css() method.
click(function(){ var class = $(this). attr("data-class"); var color = $(this). attr("data-color"); $("."+class). css("background-color",color); });
You're using a colon instead of a comma. Try:
$(body).css("background-color","blue");
You also need to wrap the id in quotes or it will look for a variable called #co
$("#co").click(change()
There are many more issues here. click
isn't an HTML attribute. You want onclick
(which is redundant). Try this:
<div id="co"> <!-- no onclick method needed -->
<script>
$(document).ready(function() {
$("#co").click(function() {
$("body").css("background-color","blue"); //edit, body must be in quotes!
});
});
</script>
You were trying to call an undefined method. It looks like you were trying to declare it inside the callback statement? I'm not sure. But please compare this to your code and see the differences.
http://jsfiddle.net/CLwE5/ demo fiddle
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With